20.2. Deploy Gunicorn and Docker
20.2.1. Dockerfile
Create file myproject/Dockerfile
with content:
FROM python:3.13
WORKDIR /data
COPY requirements.txt /tmp
COPY src /data
RUN pip install gunicorn
RUN pip install --no-cache-dir -r /tmp/requirements.txt
CMD gunicorn myproject.wsgi \
--name myproject \
--bind=0.0.0.0:8000 \
--workers=1 \
--error-logfile=/data/logs/gunicorn-error.log \
--access-logfile=/data/logs/gunicorn-access.log \
--log-level=info
20.2.2. Docker
Build:
$ docker build . -t myproject
Test:
$ docker run -it -p 8000:8000 myproject
$ open http://localhost:8000
Run:
$ docker run \
--detach \
--name myproject \
--restart always \
--network local \
--volume /home/myproject/logs:/data/logs \
myproject
20.2.3. Nginx
$ mkdir -p /home/myproject
server {
listen 80;
server_name mydomain.com www.mydomain.com;
return 301 https://mydomain.com$request_uri;
}
server {
listen 443 ssl;
server_name mydomain.com www.mydomain.com;
error_log /data/logs/mydomain.com.error warn;
access_log /data/logs/mydomain.com.access combined;
# certbot https://eff-certbot.readthedocs.io/en/latest/install.html
ssl_certificate /data/cert/live/mydomain.com/fullchain.pem;
ssl_certificate_key /data/cert/live/mydomain.com/privkey.pem;
location /static/ {
root /data/static/;
}
location /media/ {
root /data/media/;
}
location / {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://myproject:8000/;
}
}
$ sudo docker run \
--name httpd \
--detach \
--restart always \
--publish 80:80 \
--publish 443:443 \
--network local \
--volume /home/myproject/static:/data/static \
--volume /home/myproject/media:/data/media \
--volume /home/myproject/logs:/data/log \
--volume /home/myproject/cert:/data/cert:ro \
--volume /home/myproject/conf:/etc/nginx/conf.d:ro \
nginx