Skip to contentSkip to conversation

Docker: environment files for dev, staging and production

PATCH New Tim Orling 1 participant 5 messages Oct 18

lore
Share & tools
lore

Tim Orling

author
#0 lore raw

cover Docker: environment files for dev, staging and production

This series moves the Docker container settings out of docker-compose.yml into tracked .env.<context>.<service> files and adds staging and production compose setups, with two fixes found on the way.

Patch 1 stops DEBUG=0 from enabling Django's debug mode: any non-empty value used to count as true.

Patch 2 makes nginx send X-Forwarded-Proto, which docker/settings.py already trusts. Without it Django treated HTTPS requests as plain HTTP, which is also what caused the 403 on login on a non-default HTTPS port [YOCTO #15329].

Patch 3 lets DATABASE_PORT and STATIC_ROOT be set from the environment and adds ENABLE_RRS to turn on the Recipe Reporting System.

Patch 4 adds the env files and the staging and production setups. The secrets in the staging and production files are commented out, so nothing starts with a known password; the real values belong in a local commit on the server. dockersetup.py now writes the secrets it generates to untracked .env.local.* files instead of editing docker-compose.yml. Installations set up with dockersetup.py before this keep their secrets in docker-compose.yml and need them carried over once; the README change in the documentation series that follows describes how.

Tested with docker/run-tests.sh (the full suite, including tests/test_update.py) and on a test deployment.

Tim Orling (4):
docker/settings.py: don't enable DEBUG when DEBUG=0
docker: set X-Forwarded-Proto in the nginx configs docker/settings.py: read more settings from the environment Add staging and production docker-compose setups with env files

 .dockerignore              |   1 +
 .env.dev.app               |  14 +++++
 .env.dev.celery            |  14 +++++
 .env.dev.db                |   3 +
 .env.dev.rabbit            |   3 +
 .env.prod.app              |  15 +++++
 .env.prod.celery           |  15 +++++
 .env.prod.db               |   4 ++
 .env.prod.rabbit           |   4 ++
 .env.staging.app           |  15 +++++
 .env.staging.celery        |  15 +++++
 .env.staging.db            |   4 ++
 .env.staging.rabbit        |   4 ++
 .gitignore                 |   2 +
 README                     |  23 +++++++-
 docker-compose.prod.yml    |  74 +++++++++++++++++++++++++
 docker-compose.staging.yml |  74 +++++++++++++++++++++++++
 docker-compose.yml         |  42 +++-----------
 docker/nginx-ssl.conf      |   2 +
 docker/nginx.conf          |   1 +
 docker/settings.py         |   9 ++-
 dockersetup.py             | 111 +++++++++++++++++--------------------
 22 files changed, 350 insertions(+), 99 deletions(-)
 create mode 100644 .env.dev.app
 create mode 100644 .env.dev.celery
 create mode 100644 .env.dev.db
 create mode 100644 .env.dev.rabbit
 create mode 100644 .env.prod.app
 create mode 100644 .env.prod.celery
 create mode 100644 .env.prod.db
 create mode 100644 .env.prod.rabbit
 create mode 100644 .env.staging.app
 create mode 100644 .env.staging.celery
 create mode 100644 .env.staging.db
 create mode 100644 .env.staging.rabbit
 create mode 100644 docker-compose.prod.yml
 create mode 100644 docker-compose.staging.yml

base-commit: e6040597ae0f614581de241fe78f20e34dae13ac

Signature · 1 line
2.56.0
author
#1 lore raw

PATCH 1/4 docker/settings.py: don't enable DEBUG when DEBUG=0

os.getenv() returns a string, and any non-empty string is true, so DEBUG=0 (or DEBUG=False) turned debug mode on, exposing tracebacks and settings on error pages. Only enable it for 1, true or yes.

AI-Generated: Claude Opus 5.5

---
 docker/settings.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
docker/settings.py +1 -1
index 90bc5f4..c33731c 100644
@@ -7,7 +7,7 @@
 
 import os
 
-DEBUG = os.getenv('DEBUG', False)
+DEBUG = os.getenv('DEBUG', '0').lower() in ('1', 'true', 'yes')
 
 ADMINS = (
     # ('Your Name', '[email protected]'),
Signature · 1 line
2.56.0
author
#2 lore raw

PATCH 2/4 docker: set X-Forwarded-Proto in the nginx configs

In reply to Tim Orling

docker/settings.py sets SECURE_PROXY_SSL_HEADER to trust X-Forwarded-Proto, but nginx never sent it, so Django treated every request as plain HTTP even when served over HTTPS: request.is_secure() was false and absolute URLs it built used http://. Set it from $scheme, which also overwrites any value the client sends.

This also fixes the 403 on login when the site is served on a non-default HTTPS port such as https://localhost:8081: Django now sees the request as secure, so the browser's Origin matches the request without listing the port in CSRF_TRUSTED_ORIGINS.

Fixes [YOCTO #15329]

AI-Generated: Claude Opus 5.5

---
 docker/nginx-ssl.conf | 2 ++
 docker/nginx.conf     | 1 +
 2 files changed, 3 insertions(+)
docker/nginx-ssl.conf +2 -0
index 9776b8f..75f01ee 100644
@@ -70,6 +70,7 @@ http {
 
         location @proxy_to_app {
             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
+            proxy_set_header X-Forwarded-Proto $scheme;
             proxy_set_header Host $http_host;
             proxy_redirect off;
 
@@ -132,6 +133,7 @@ http {
 
         location @proxy_to_app {
             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
+            proxy_set_header X-Forwarded-Proto $scheme;
             proxy_set_header Host $http_host;
             proxy_redirect off;
 
docker/nginx.conf +1 -0
index eda3474..0a47a1c 100644
@@ -37,6 +37,7 @@ http {
 
         location @proxy_to_app {
             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
+            proxy_set_header X-Forwarded-Proto $scheme;
             proxy_set_header Host $http_host;
             proxy_redirect off;
 
Signature · 1 line
2.56.0
author
#3 lore raw

PATCH 3/4 docker/settings.py: read more settings from the environment

In reply to Tim Orling

Let DATABASE_PORT (default 3306) and STATIC_ROOT be overridden from the environment, and add ENABLE_RRS: set it to 1 to add the 'rrs' (Recipe Reporting System) app to INSTALLED_APPS. It is off by default.

AI-Generated: Claude Opus 5.5

---
 docker/settings.py | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
docker/settings.py +5 -2
index c33731c..820c4bb 100644
@@ -22,7 +22,7 @@ DATABASES = {
         'USER': os.getenv('DATABASE_USER', 'root'),
         'PASSWORD': os.getenv('DATABASE_PASSWORD', 'testingpw'),
         'HOST': os.getenv('DATABASE_HOST', 'layersdb'),
-        'PORT': '',
+        'PORT': os.getenv('DATABASE_PORT', '3306'),
     }
 }
 
@@ -65,7 +65,7 @@ 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 = '/usr/share/nginx/html/static/'
+STATIC_ROOT = os.getenv('STATIC_ROOT', '/usr/share/nginx/html/static/')
 
 # URL prefix for static files.
 # Example: "http://media.lawrence.com/static/"
@@ -164,6 +164,9 @@ INSTALLED_APPS = (
     'corsheaders',
 )
 
+if os.getenv('ENABLE_RRS', '0') == '1':
+    INSTALLED_APPS += ('rrs',)
+
 AUTHENTICATION_BACKENDS = [
     'axes.backends.AxesBackend',
     'django.contrib.auth.backends.ModelBackend',
Signature · 1 line
2.56.0
author
#4 lore raw

PATCH 4/4 Add staging and production docker-compose setups with env files

In reply to Tim Orling

Move the container settings out of docker-compose.yml into tracked .env.<context>.<service> files, so local configuration no longer means editing the compose file and fighting upstream changes to it. docker-compose.yml uses the .env.dev.* files, which keep the existing development-only values.

Add docker-compose.staging.yml and docker-compose.prod.yml, which use .env.staging.* and .env.prod.*, serve ports 80 and 443, run certbot, and keep the database in a volume. Their secrets are commented out, so nothing starts with a known password; the real values belong in a local commit on the server. Keep the .env* files out of the Docker images.

dockersetup.py used to write the generated secret key, database and RabbitMQ passwords and email settings into the environment entries of docker-compose.yml. It now copies each .env.dev.<service> to .env.local.<service> (mode 0600) with those values filled in and DEBUG off, and points docker-compose.yml at the copies. Update mode reads the root password back from .env.local.db, or from docker-compose.yml for installations made before this change. Git ignores the .env.local.* files and the .env file that dockersetup.py already writes.

AI-Generated: Claude Opus 5.5

---
 .dockerignore              |   1 +
 .env.dev.app               |  14 +++++
 .env.dev.celery            |  14 +++++
 .env.dev.db                |   3 +
 .env.dev.rabbit            |   3 +
 .env.prod.app              |  15 +++++
 .env.prod.celery           |  15 +++++
 .env.prod.db               |   4 ++
 .env.prod.rabbit           |   4 ++
 .env.staging.app           |  15 +++++
 .env.staging.celery        |  15 +++++
 .env.staging.db            |   4 ++
 .env.staging.rabbit        |   4 ++
 .gitignore                 |   2 +
 README                     |  23 +++++++-
 docker-compose.prod.yml    |  74 +++++++++++++++++++++++++
 docker-compose.staging.yml |  74 +++++++++++++++++++++++++
 docker-compose.yml         |  42 +++-----------
 dockersetup.py             | 111 +++++++++++++++++--------------------
 19 files changed, 341 insertions(+), 96 deletions(-)
 create mode 100644 .env.dev.app
 create mode 100644 .env.dev.celery
 create mode 100644 .env.dev.db
 create mode 100644 .env.dev.rabbit
 create mode 100644 .env.prod.app
 create mode 100644 .env.prod.celery
 create mode 100644 .env.prod.db
 create mode 100644 .env.prod.rabbit
 create mode 100644 .env.staging.app
 create mode 100644 .env.staging.celery
 create mode 100644 .env.staging.db
 create mode 100644 .env.staging.rabbit
 create mode 100644 docker-compose.prod.yml
 create mode 100644 docker-compose.staging.yml
.dockerignore +1 -0
index 3b82016..e689d37 100644
@@ -19,3 +19,4 @@ dockersetup.py
 .venv
 .idea
 .DS_Store
+.env*
.env.dev.app new file +14 -0
new file mode 100644
index 0000000..9f8fb5a
@@ -0,0 +1,14 @@
+# Development only; not secret.
+#SECRET_KEY=<set this here>
+DATABASE_USER=root
+DATABASE_PASSWORD=testingpw
+DATABASE_HOST=layersdb
+RABBITMQ_DEFAULT_USER=guest
+RABBITMQ_DEFAULT_PASS=guest
+#EMAIL_HOST=<set this here>
+#EMAIL_PORT=<set this here if not the default>
+#EMAIL_USER=<set this here if needed>
+#EMAIL_PASSWORD=<set this here if needed>
+#EMAIL_USE_SSL=<set this here if needed>
+#EMAIL_USE_TLS=<set this here if needed>
+DEBUG=1
.env.dev.celery new file +14 -0
new file mode 100644
index 0000000..cb07c1c
@@ -0,0 +1,14 @@
+# Development only; not secret.
+#SECRET_KEY=<set this here>
+DATABASE_USER=layers
+DATABASE_PASSWORD=testingpw
+DATABASE_HOST=layersdb
+RABBITMQ_DEFAULT_USER=guest
+RABBITMQ_DEFAULT_PASS=guest
+#EMAIL_HOST=<set this here>
+#EMAIL_PORT=<set this here if not the default>
+#EMAIL_USER=<set this here if needed>
+#EMAIL_PASSWORD=<set this here if needed>
+#EMAIL_USE_SSL=<set this here if needed>
+#EMAIL_USE_TLS=<set this here if needed>
+#DEBUG=1
.env.dev.db new file +3 -0
new file mode 100644
index 0000000..90c6252
@@ -0,0 +1,3 @@
+# Development only; not secret.
+MYSQL_DATABASE=layersdb
+MYSQL_ROOT_PASSWORD=testingpw
.env.dev.rabbit new file +3 -0
new file mode 100644
index 0000000..41a5eeb
@@ -0,0 +1,3 @@
+# Development only; not secret.
+RABBITMQ_DEFAULT_USER=guest
+RABBITMQ_DEFAULT_PASS=guest
.env.prod.app new file +15 -0
new file mode 100644
index 0000000..0160450
@@ -0,0 +1,15 @@
+# Set real values in a local commit on the server; never push them.
+# Without SECRET_KEY the application refuses to start.
+#SECRET_KEY=<set this here>
+DATABASE_USER=root
+#DATABASE_PASSWORD=<set this here>
+DATABASE_HOST=layersdb
+#RABBITMQ_DEFAULT_USER=<set this here>
+#RABBITMQ_DEFAULT_PASS=<set this here>
+#EMAIL_HOST=<set this here>
+#EMAIL_PORT=<set this here if not the default>
+#EMAIL_USER=<set this here if needed>
+#EMAIL_PASSWORD=<set this here if needed>
+#EMAIL_USE_SSL=<set this here if needed>
+#EMAIL_USE_TLS=<set this here if needed>
+DEBUG=0
.env.prod.celery new file +15 -0
new file mode 100644
index 0000000..e2e426f
@@ -0,0 +1,15 @@
+# Set real values in a local commit on the server; never push them.
+# Use the same values as in .env.prod.app.
+#SECRET_KEY=<set this here>
+DATABASE_USER=layers
+#DATABASE_PASSWORD=<set this here>
+DATABASE_HOST=layersdb
+#RABBITMQ_DEFAULT_USER=<set this here>
+#RABBITMQ_DEFAULT_PASS=<set this here>
+#EMAIL_HOST=<set this here>
+#EMAIL_PORT=<set this here if not the default>
+#EMAIL_USER=<set this here if needed>
+#EMAIL_PASSWORD=<set this here if needed>
+#EMAIL_USE_SSL=<set this here if needed>
+#EMAIL_USE_TLS=<set this here if needed>
+#DEBUG=1
.env.prod.db new file +4 -0
new file mode 100644
index 0000000..99232ab
@@ -0,0 +1,4 @@
+# Set real values in a local commit on the server; never push them.
+# Without MYSQL_ROOT_PASSWORD the database refuses to start.
+MYSQL_DATABASE=layersdb
+#MYSQL_ROOT_PASSWORD=<set this here>
.env.prod.rabbit new file +4 -0
new file mode 100644
index 0000000..b422a9a
@@ -0,0 +1,4 @@
+# Set real values in a local commit on the server; never push them.
+# Use the same values as RABBITMQ_* in .env.prod.app.
+#RABBITMQ_DEFAULT_USER=<set this here>
+#RABBITMQ_DEFAULT_PASS=<set this here>
.env.staging.app new file +15 -0
new file mode 100644
index 0000000..0160450
@@ -0,0 +1,15 @@
+# Set real values in a local commit on the server; never push them.
+# Without SECRET_KEY the application refuses to start.
+#SECRET_KEY=<set this here>
+DATABASE_USER=root
+#DATABASE_PASSWORD=<set this here>
+DATABASE_HOST=layersdb
+#RABBITMQ_DEFAULT_USER=<set this here>
+#RABBITMQ_DEFAULT_PASS=<set this here>
+#EMAIL_HOST=<set this here>
+#EMAIL_PORT=<set this here if not the default>
+#EMAIL_USER=<set this here if needed>
+#EMAIL_PASSWORD=<set this here if needed>
+#EMAIL_USE_SSL=<set this here if needed>
+#EMAIL_USE_TLS=<set this here if needed>
+DEBUG=0
.env.staging.celery new file +15 -0
new file mode 100644
index 0000000..445226a
@@ -0,0 +1,15 @@
+# Set real values in a local commit on the server; never push them.
+# Use the same values as in .env.staging.app.
+#SECRET_KEY=<set this here>
+DATABASE_USER=layers
+#DATABASE_PASSWORD=<set this here>
+DATABASE_HOST=layersdb
+#RABBITMQ_DEFAULT_USER=<set this here>
+#RABBITMQ_DEFAULT_PASS=<set this here>
+#EMAIL_HOST=<set this here>
+#EMAIL_PORT=<set this here if not the default>
+#EMAIL_USER=<set this here if needed>
+#EMAIL_PASSWORD=<set this here if needed>
+#EMAIL_USE_SSL=<set this here if needed>
+#EMAIL_USE_TLS=<set this here if needed>
+#DEBUG=1
.env.staging.db new file +4 -0
new file mode 100644
index 0000000..99232ab
@@ -0,0 +1,4 @@
+# Set real values in a local commit on the server; never push them.
+# Without MYSQL_ROOT_PASSWORD the database refuses to start.
+MYSQL_DATABASE=layersdb
+#MYSQL_ROOT_PASSWORD=<set this here>
.env.staging.rabbit new file +4 -0
new file mode 100644
index 0000000..36a26e4
@@ -0,0 +1,4 @@
+# Set real values in a local commit on the server; never push them.
+# Use the same values as RABBITMQ_* in .env.staging.app.
+#RABBITMQ_DEFAULT_USER=<set this here>
+#RABBITMQ_DEFAULT_PASS=<set this here>
.gitignore +2 -0
index 6b271d4..814f9d1 100644
@@ -18,3 +18,5 @@ requirements.txt.updated
 requirements_outdated.txt
 requirements_safety_check.txt
 requirements_deptree.txt
+.env
+.env.local.*
README +22 -1
index 96c2ddd..3f733af 100644
@@ -75,7 +75,28 @@ instructions will walk you through the setup.
 
    Note that updating with -u/--update will only work if the configuration
    changes originally made by dockersetup.py upon installation (e.g. passwords,
-   hostname, etc.) are still present in the source tree.
+   hostname, etc.) are still present in the source tree. The generated
+   passwords and secret key are written to .env.local.* files, which git
+   ignores; keep them, and do not commit them.
+
+Environment files:
+
+The containers read their settings (secret key, database and RabbitMQ
+credentials, email server, DEBUG) from .env.<context>.<service> files, where
+<context> is dev, staging or prod and <service> is app, celery, db or rabbit:
+
+  - docker-compose.yml uses the .env.dev.* files, which hold development-only
+    values. dockersetup.py copies them to .env.local.*, fills in the secrets
+    it generates, and points docker-compose.yml at those copies.
+  - docker-compose.staging.yml and docker-compose.prod.yml use the
+    .env.staging.* and .env.prod.* files. Their secrets are commented out, so
+    nothing starts with a known password. Set the real values, and the
+    hostname in the compose file, in a local commit on the server and never
+    push them. Then run, for example:
+
+        docker compose -f docker-compose.prod.yml up -d --build
+
+The .env* files are excluded from the Docker images.
 
 
 
docker-compose.prod.yml new file +74 -0

74 lines not on this page · raw message

docker-compose.staging.yml new file +74 -0

74 lines not on this page · raw message

docker-compose.yml +8 -34

66 lines not on this page · raw message

dockersetup.py +50 -61

141 lines not on this page · raw message

Signature · 1 line
2.56.0