Сборка Docker на сервере Ubuntu: Ошибки нет, добро пожаловать в nginx! Но нет Django
Выпуск:
Я настраиваю свой сервер Ubuntu 24.04 LTS (GNU/Linux 6.8.0-36-generic x86_64) для работы с моим Django-сайтом. Мы собираемся запустить все в среде docker.
docker-compose up --build
Все собирается без сообщений об ошибках, но теперь мы видим только этот экран:
Идеальный исход:
Удалось получить доступ к сайту django через домен, а затем успешно настроить https через letsencrypt.
Релевантный код:
file: /var/www/mydjangosite/config/gunicorn.conf.py
import multiprocessing
bind = "0.0.0.0:9000"
workers = multiprocessing.cpu_count() * 2 + 1
loglevel = 'info'
errorlog = '-'
accesslog = '-'
file: /var/www/mydjangosite/config/nginx.conf
# HTTP Server Block (Port 80)
server {
listen 80;
server_name mydjangosite.com;
location /.well-known/acme-challenge/ {
root /var/www/mydjangosite;
}
location / {
return 301 https://$host$request_uri;
}
}
# HTTPS Server Block (Port 443)
server {
listen 443 ssl;
server_name mydjangosite.com www.mydjangosite.com;
ssl_certificate /etc/letsencrypt/live/mydjangosite.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/mydjangosite.com/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
location /.well-known/acme-challenge/ {
root /var/www/mydjangosite;
}
location / {
proxy_pass http://web:9000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
location /static/ {
alias /staticfiles/;
}
location /media/ {
alias /var/www/mydjangosite/media/;
}
}
# Redirect Non-www to www
server {
listen 80;
server_name mydjangosite.com;
return 301 http://www.mydjangosite.com$request_uri;
}
# Redirect HTTP to HTTPS for www
server {
listen 80;
server_name www.mydjangosite.com;
location /.well-known/acme-challenge/ {
root /var/www/mydjangosite;
}
return 301 https://www.mydjangosite.com$request_uri;
}
file: /var/www/mydjangosite/docker-compose.yml
# version: '3.8' # not required
volumes:
postgres_data: {}
static_volume: {}
services:
nginx:
image: nginx:latest
env_file:
- .env
volumes:
- ./.env:/code/.env # I've tried every variation of this
- ./config/nginx.conf:/etc/nginx/nginx.conf
- static_volume:/staticfiles
ports:
- "80:80"
depends_on:
- web
command: nginx -g 'daemon off;'
web:
build:
context: .
# command: gunicorn mydjangosite.wsgi:application --config ./config/gunicorn.conf.py
command: >
bash -c "
cat .env &&
pip install --default-timeout=100 --retries=5 -r requirements.txt &&
python manage.py migrate &&
python manage.py runserver 0.0.0.0:9000"
environment:
- DATABASE_URL=postgres://${DBUSER}:${DBPASSWORD}@${DBHOST}:${DBPORT}/${DBNAME}
env_file:
- .env
volumes:
- .:/code
- static_volume:/code/staticfiles
ports:
- "9000:9000"
# LOCAL DATABASE: Uncomment the following lines if you are using a local database
# depends_on:
# - db
redis:
image: redis:latest
ports:
- "6379:6379"
celery:
build:
context: .
command: celery -A mydjangosite worker --loglevel=info -B
volumes:
- .:/code
environment:
- DATABASE_URL=postgres://${DBUSER}:${DBPASSWORD}@${DBHOST}:${DBPORT}/${DBNAME}
env_file:
- .env
depends_on:
- web
- redis
file: /var/www/mydjangosite/Dockerfile
# Use an official Python runtime as a parent image
FROM python:latest
# Set environment variables
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
# Set the working directory in the container
WORKDIR /code
# Copy the project code
COPY . /code/
# Install any needed packages specified in requirements.txt
RUN apt-get update && apt-get install -y libxslt-dev libxml2-dev libpam-dev libedit-dev && apt-get clean && rm -rf /var/lib/apt/lists/*
# RUN pip install --upgrade pip && pip install -r requirements.txt
RUN pip install --upgrade pip && pip install --default-timeout=100 --retries=5 -r requirements.txt
RUN python manage.py collectstatic --no-input
# Collect static files
RUN python manage.py collectstatic --no-input
# Command to run the Django application
CMD ["gunicorn", "mydjangosite.wsgi:application", "--bind", "0.0.0.0:9000"]