Deploy with gunicorn behind nginx
cover Deploy with gunicorn behind nginx
Align docker-compose with layerindex-web and deploy with gunicorn behind an nginx reverse-proxy. We may move to uwsgi (better handles static pages) and Caddy (better handles Let's Encrypt certs) in the future, but for now set up better best practices.
Move the .env setup to dev, prod and staging flavors. The difference between prod and staging is that Let's Encrypt staging server is used for staging.
Add docker/nginx*.conf, similar to what is in layerindex-web and provide a settings.py.docker example file.
We may need to align settings.py better with production, but for now this is an improvement.
Tim Orling (4):
Run gunicorn behind nginx in docker-compose.
Add staging and production docker-compose setups with env files. Add a VS Code devcontainer.
Remove the obsolete .env.
.devcontainer/devcontainer.json | 63 ++++++++++++++++++++++++ .devcontainer/docker-compose.dev.yml | 15 ++++++ .dockerignore | 2 + .env | 5 -- .env.dev.app | 8 ++++ .env.dev.db | 5 ++ .env.prod.app | 9 ++++ .env.prod.db | 6 +++ .env.staging.app | 9 ++++ .env.staging.db | 6 +++ .gitignore | 1 + Dockerfile | 2 +- README | 39 +++++++++++++++ docker-compose.prod.yaml | 55 +++++++++++++++++++++ docker-compose.staging.yaml | 55 +++++++++++++++++++++ docker-compose.yaml | 32 ++++++++----- docker/nginx-ssl.conf | 72 ++++++++++++++++++++++++++++ docker/nginx.conf | 49 +++++++++++++++++++ project/settings.py.docker | 3 +- 19 files changed, 418 insertions(+), 18 deletions(-) create mode 100644 .devcontainer/devcontainer.json create mode 100644 .devcontainer/docker-compose.dev.yml delete mode 100644 .env create mode 100644 .env.dev.app create mode 100644 .env.dev.db create mode 100644 .env.prod.app create mode 100644 .env.prod.db create mode 100644 .env.staging.app create mode 100644 .env.staging.db create mode 100644 docker-compose.prod.yaml create mode 100644 docker-compose.staging.yaml create mode 100644 docker/nginx-ssl.conf create mode 100644 docker/nginx.conf
base-commit: 752b7188d04c2fd0e2f32f0cd2dd437c43058d82
prerequisite-patch-id: 9c9d012e1163dd1d335531d0451657823f17eb64
prerequisite-patch-id: ddb4df3e54736ed25846f91d4918299e601f4071
prerequisite-patch-id: ab69f4aa494291611cbe0e916f18b07f4aeebe57
prerequisite-patch-id: ac2b802ea065536df3854c48e265f5508d201cff
prerequisite-patch-id: 81a7de0e9568848efc8c66d397abb5f56218db95
prerequisite-patch-id: cf32dcaef20903890a020211a35f46b2384207da
prerequisite-patch-id: 6920bc5c70071bbd42b55b08bfc2bff04133c3ab
prerequisite-patch-id: a72bee4356db80ccb45321967d18081961f60860
prerequisite-patch-id: a2285b682329ba20d838085588b7c6681a86d273
prerequisite-patch-id: c3980e46d49b00b419cf4a7c2bc32c346478a453
prerequisite-patch-id: 627f58daa68852066f56558f08b07ad4af7bef9f
prerequisite-patch-id: 2d1b78495c909de035dbed74af06db5683701d1b
prerequisite-patch-id: 028165edb39ba03ad70f83ae9e2f36dff8c7e912
Signature · 1 line
2.56.0
PATCH 1/4 Run gunicorn behind nginx in docker-compose.
As in layerindex-web, nginx serves static files and proxies to gunicorn. Unlike it, nginx sets X-Forwarded-Proto and accepts 10MB reports. Drop the obsolete compose version key.
AI-Generated: Claude Opus 5.5
--- Dockerfile | 2 +- docker-compose.yaml | 20 ++++++++++++---- docker/nginx.conf | 49 ++++++++++++++++++++++++++++++++++++++ project/settings.py.docker | 3 ++- 4 files changed, 68 insertions(+), 6 deletions(-) create mode 100644 docker/nginx.conf
Dockerfile +1 -1
@@ -10,7 +10,7 @@ WORKDIR /app # Install dependencies COPY requirements.txt /app/ -RUN pip install -r requirements.txt +RUN pip install -r requirements.txt gunicorn==26.2.0 # Copy the project code into the container COPY . /app/
docker-compose.yaml +16 -4
@@ -1,4 +1,3 @@ -version: '3' services: db: image: mysql:8.4
@@ -23,14 +22,27 @@ services: context: . dockerfile: Dockerfile container_name: error_report_web_backend - command: sh -c "python3 manage.py migrate --noinput && python3 manage.py runserver 0.0.0.0:8000" + command: sh -c "python3 manage.py migrate --noinput && python3 manage.py collectstatic --noinput && gunicorn project.wsgi --bind=:8000 --workers=4 --timeout=60" restart: always - ports: - - "8000:8000" + volumes: + - static:/app/staticfiles env_file: - .env depends_on: db: condition: service_healthy + + web: + image: nginx:1.30 + container_name: error_report_web_nginx + restart: always + ports: + - "8080:80" + volumes: + - ./docker/nginx.conf:/etc/nginx/nginx.conf:ro + - static:/usr/share/nginx/html/static:ro + depends_on: + - backend volumes: data: + static:
docker/nginx.conf new file +49 -0
49 lines not on this page · raw message
project/settings.py.docker +2 -1
@@ -105,7 +105,8 @@ MEDIA_URL = '' # Don't put anything in this directory yourself; store your static files # in apps' "static/" subdirectories and in STATICFILES_DIRS. # Example: "/home/media/media.lawrence.com/static/" -STATIC_ROOT = '' +# Shared with nginx. +STATIC_ROOT = '/app/staticfiles' # URL prefix for static files. # Example: "http://media.lawrence.com/static/"
Signature · 1 line
2.56.0
PATCH 2/4 Add staging and production docker-compose setups with env files.
In reply to Tim OrlingFollow layerindex-web's timo/docker-compose-env branch: tracked .env.{dev,staging,prod}.{app,db}, with real values in a local commit on the server. Staging and prod leave secrets commented out, so nothing starts with a known password, and serve HTTPS with Let's Encrypt (staging from its staging server); certbot renews. /ClientPost/ stays on HTTP, as send-error-report may post there. The db healthcheck no longer needs the root password. Keep docker/certs out of git and the env files out of the image.
AI-Generated: Claude Opus 5.5
--- .dockerignore | 2 ++ .env.dev.app | 8 +++++ .env.dev.db | 5 +++ .env.prod.app | 9 +++++ .env.prod.db | 6 ++++ .env.staging.app | 9 +++++ .env.staging.db | 6 ++++ .gitignore | 1 + README | 39 ++++++++++++++++++++ docker-compose.prod.yaml | 55 ++++++++++++++++++++++++++++ docker-compose.staging.yaml | 55 ++++++++++++++++++++++++++++ docker-compose.yaml | 12 +++---- docker/nginx-ssl.conf | 72 +++++++++++++++++++++++++++++++++++++ 13 files changed, 272 insertions(+), 7 deletions(-) create mode 100644 .env.dev.app create mode 100644 .env.dev.db create mode 100644 .env.prod.app create mode 100644 .env.prod.db create mode 100644 .env.staging.app create mode 100644 .env.staging.db create mode 100644 docker-compose.prod.yaml create mode 100644 docker-compose.staging.yaml create mode 100644 docker/nginx-ssl.conf
.dockerignore +2 -0
@@ -8,3 +8,5 @@ __pycache__ *.patch venv .venv +.env.* +docker/certs
.env.dev.app new file +8 -0
@@ -0,0 +1,8 @@ +# Development only; not secret. +SECRET_KEY=dev-only-not-secret +DEBUG=1 +ALLOWED_HOSTS=localhost,127.0.0.1 +MYSQL_DATABASE=errorreport +MYSQL_USER=errorreport +MYSQL_PASSWORD=testingpw +#CSP_REPORT_ONLY=1
.env.dev.db new file +5 -0
@@ -0,0 +1,5 @@ +# Development only; not secret. +MYSQL_DATABASE=errorreport +MYSQL_USER=errorreport +MYSQL_PASSWORD=testingpw +MYSQL_ROOT_PASSWORD=testingpw
.env.prod.app new file +9 -0
@@ -0,0 +1,9 @@ +# Set real values in a local commit on the server; never push them. +# Commented-out secrets stop the service starting. +#SECRET_KEY=<set this here> +DEBUG=0 +ALLOWED_HOSTS=errors.yoctoproject.org +MYSQL_DATABASE=errorreport +MYSQL_USER=errorreport +#MYSQL_PASSWORD=<set this here, same as in .env.prod.db> +#CSP_REPORT_ONLY=1
.env.prod.db new file +6 -0
@@ -0,0 +1,6 @@ +# Set real values in a local commit on the server; never push them. +# Commented-out secrets stop the service starting. +MYSQL_DATABASE=errorreport +MYSQL_USER=errorreport +#MYSQL_PASSWORD=<set this here, same as in .env.prod.app> +#MYSQL_ROOT_PASSWORD=<set this here>
.env.staging.app new file +9 -0
@@ -0,0 +1,9 @@ +# Set real values in a local commit on the server; never push them. +# Commented-out secrets stop the service starting. +#SECRET_KEY=<set this here> +DEBUG=0 +#ALLOWED_HOSTS=<set this here> +MYSQL_DATABASE=errorreport +MYSQL_USER=errorreport +#MYSQL_PASSWORD=<set this here, same as in .env.staging.db> +#CSP_REPORT_ONLY=1
.env.staging.db new file +6 -0
@@ -0,0 +1,6 @@ +# Set real values in a local commit on the server; never push them. +# Commented-out secrets stop the service starting. +MYSQL_DATABASE=errorreport +MYSQL_USER=errorreport +#MYSQL_PASSWORD=<set this here, same as in .env.staging.app> +#MYSQL_ROOT_PASSWORD=<set this here>
.gitignore +1 -0
@@ -8,3 +8,4 @@ pull-*/ venv *.sqlite3 .env +docker/certs/
README +39 -0
@@ -41,6 +41,45 @@ In order to send an error report to the server, run the "send-error-report file_ For more information on error reporting and log collection see: http://www.yoctoproject.org/docs/current/dev-manual/dev-manual.html#using-the-error-reporting-tool +Docker +------ + +docker-compose.yaml runs a development setup: MySQL, the application in +gunicorn, and nginx serving it on http://localhost:8080. Its settings are in +.env.dev.app and .env.dev.db, which hold development-only values. + + docker compose up -d --build + +To run the tests in a container, as CI does: + + docker/run-tests.sh + +docker-compose.staging.yaml and docker-compose.prod.yaml serve the site over +HTTPS on ports 80 and 443, with a certificate from Let's Encrypt. Staging uses +Let's Encrypt's staging server, whose certificates browsers don't trust but +whose rate limits are generous; production uses the real server. Taking prod +as the example: + +1. Set the real values in .env.prod.app and .env.prod.db (SECRET_KEY, + ALLOWED_HOSTS and the database passwords) in a local commit on the server. + Never push them. +2. Get the first certificate. nginx needs it to start, so certbot answers the + challenge itself on port 80 (for staging, use docker-compose.staging.yaml + and add --test-cert): + + docker compose -f docker-compose.prod.yaml run --rm -p 80:80 \ + --entrypoint certbot certbot certonly --standalone \ + --cert-name error-report-web -d errors.example.org \ + -m [email protected] --agree-tos --no-eff-email + + The certificate is stored in docker/certs, which git ignores. +3. Start everything: + + docker compose -f docker-compose.prod.yaml up -d --build + +The certbot container renews the certificate from the server that issued it, +and nginx reloads every 6 hours to pick it up. + Content Security Policy -----------------------
docker-compose.prod.yaml new file +55 -0
55 lines not on this page · raw message
docker-compose.staging.yaml new file +55 -0
55 lines not on this page · raw message
docker-compose.yaml +5 -7
@@ -5,15 +5,13 @@ services: restart: always volumes: - data:/var/lib/mysql - environment: - MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD} - MYSQL_DATABASE: ${MYSQL_DATABASE} - MYSQL_USER: ${MYSQL_USER} - MYSQL_PASSWORD: ${MYSQL_PASSWORD} + env_file: + - .env.dev.db ports: - "3306:3306" healthcheck: - test: ["CMD", "mysql", "-h", "localhost", "-u", "root", "-p${MYSQL_ROOT_PASSWORD}", "-e", "SELECT 1"] + # TCP: MySQL's init-time server only listens on the socket. + test: ["CMD", "mysqladmin", "ping", "-h", "127.0.0.1", "--silent"] timeout: 20s retries: 10
@@ -27,7 +25,7 @@ services: volumes: - static:/app/staticfiles env_file: - - .env + - .env.dev.app depends_on: db: condition: service_healthy
docker/nginx-ssl.conf new file +72 -0
72 lines not on this page · raw message
Signature · 1 line
2.56.0
PATCH 3/4 Add a VS Code devcontainer.
In reply to Tim OrlingLayered on docker-compose.yaml, as layerindex-web's is: runserver on the mounted workspace, MySQL unpublished, no nginx. Ruff formats changed lines only.
AI-Generated: Claude Opus 5.5
--- .devcontainer/devcontainer.json | 63 ++++++++++++++++++++++++++++ .devcontainer/docker-compose.dev.yml | 15 +++++++ 2 files changed, 78 insertions(+) create mode 100644 .devcontainer/devcontainer.json create mode 100644 .devcontainer/docker-compose.dev.yml
.devcontainer/devcontainer.json new file +63 -0
@@ -0,0 +1,63 @@ +{ + "name": "error-report-web (Django + MySQL)", + "dockerComposeFile": [ + "../docker-compose.yaml", + "docker-compose.dev.yml" + ], + "service": "backend", + "workspaceFolder": "/app", + "shutdownAction": "stopCompose", + "remoteUser": "root", + + // requirements.txt may have changed since the image was built. + "postCreateCommand": "pip install --no-cache-dir -r requirements.txt ruff==0.17.0 && python3 manage.py migrate --noinput", + + "forwardPorts": [ + 8000 + ], + + "customizations": { + "vscode": { + "extensions": [ + "ms-python.python", + "ms-python.vscode-pylance", + "ms-python.debugpy", + "charliermarsh.ruff", + "batisteo.vscode-django", + "mtxr.sqltools", + "mtxr.sqltools-driver-mysql" + ], + "settings": { + "python.defaultInterpreterPath": "/usr/local/bin/python", + "python.terminal.activateEnvironment": false, + "remote.autoForwardPorts": false, + // Values from .env.dev.db. + "sqltools.connections": [ + { + "mysqlOptions": { + "authProtocol": "default", + "enableSsl": "Disabled" + }, + "previewLimit": 50, + "server": "db", + "port": 3306, + "driver": "MySQL", + "name": "errorreport", + "database": "errorreport", + "username": "errorreport", + "password": "testingpw" + } + ], + // Format changed lines only. + "editor.formatOnSaveMode": "modifications", + "[python]": { + "editor.defaultFormatter": "charliermarsh.ruff" + }, + "files.exclude": { + "**/__pycache__": true, + "**/*.pyc": true + } + } + } + } +}
.devcontainer/docker-compose.dev.yml new file +15 -0
@@ -0,0 +1,15 @@ +# Devcontainer override for docker-compose.yaml: runserver on the mounted +# workspace, MySQL port unpublished (no clash with other dev DBs), no nginx. +services: + backend: + command: ["python3", "manage.py", "runserver", "0.0.0.0:8000"] + restart: "no" + volumes: + - .:/app:cached + + db: + restart: "no" + ports: !reset [] + + web: + profiles: [nginx]
Signature · 1 line
2.56.0
PATCH 4/4 Remove the obsolete .env.
In reply to Tim Orlingdocker-compose.yaml now uses .env.dev.*; nothing reads it.
AI-Generated: Claude Opus 5.5
--- .env | 5 ----- 1 file changed, 5 deletions(-) delete mode 100644 .env
.env deleted +0 -5
@@ -1,5 +0,0 @@ -# MySQL settings -MYSQL_ROOT_PASSWORD=root -MYSQL_DATABASE=errorreport_db -MYSQL_USER=errorreport -MYSQL_PASSWORD=errorreport
Signature · 1 line
2.56.0