Dev and prod environments
The project runs in two environments, each on its own machine. Dev is for checking an integration; prod is what users see.
The differences
| dev | prod | |
|---|---|---|
| Machine | VM trinity-dev | VM trinity-prod |
| Branch | develop | main |
| Addresses | dev.trinity.jostud.com, api.dev.trinity.jostud.com | trinity.jostud.com, api.trinity.jostud.com, docs.trinity.jostud.com |
| File | infra/docker-compose.dev.yml | infra/docker-compose.prod.yml |
| Started with | docker compose up -d --build | docker stack deploy (Swarm) |
| Images | built on the server from Dockerfile.dev | pulled from the registry: api:<sha>, web:<sha> |
| API server | Django's runserver | Gunicorn, 3 workers |
| Web server | next dev | node server.js (standalone build) |
Django DEBUG | True: detailed error pages | False |
| User in the containers | root | non-root (appuser, nextjs) |
| Docker health check | no | yes |
| Update | containers recreated: short outage | new version started before the old one stops: zero downtime |
| Failed update | the job fails, the containers stay broken | automatic rollback to the previous version |
| Migrations | api service command: migrate then runserver | entrypoint.sh: migrate then collectstatic |
| Auto restart | restart: unless-stopped | handled by Swarm |
| Database | PostgreSQL 17, pgdata volume | PostgreSQL 17, pgdata volume |
The dev file
yml
services:
db:
image: postgres:17-alpine
restart: unless-stopped
environment:
POSTGRES_DB: trinity
POSTGRES_USER: trinity
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
volumes:
- pgdata:/var/lib/postgresql/data
networks:
- internal
healthcheck:
test: ["CMD-SHELL", "pg_isready -U trinity -d trinity"]
interval: 5s
timeout: 3s
retries: 10
api:
build:
context: ../api
dockerfile: Dockerfile.dev
container_name: trinity_api
restart: unless-stopped
command: sh -c "python manage.py migrate && python manage.py runserver 0.0.0.0:8000"
environment:
DEBUG: "True"
SECRET_KEY: ${DJANGO_SECRET_KEY}
ALLOWED_HOSTS: api.dev.trinity.jostud.com
DB_HOST: db
DB_NAME: trinity
DB_USER: trinity
DB_PASSWORD: ${POSTGRES_PASSWORD}
networks:
- trinity
- internal
depends_on:
db:
condition: service_healthy
web:
build:
context: ../web
dockerfile: Dockerfile.dev
container_name: trinity_web
restart: unless-stopped
environment:
API_URL: https://api.dev.trinity.jostud.com
networks:
- trinity
volumes:
pgdata:
networks:
trinity:
external: true
internal:What to notice:
build:: dev rebuilds the images from the commit's code, with the dev Dockerfiles.depends_on: condition: service_healthy: the API waits for PostgreSQL to answerpg_isready.${POSTGRES_PASSWORD},${DJANGO_SECRET_KEY}: filled from GitLab's CI/CD variables at job time. See Secrets and access.container_name: setstrinity_apiandtrinity_web, the names Traefik expects.
The prod file
yml
services:
db:
image: postgres:17-alpine
environment:
POSTGRES_DB: trinity
POSTGRES_USER: trinity
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
volumes:
- pgdata:/var/lib/postgresql/data
networks:
- internal
api:
image: ${CI_REGISTRY_IMAGE}/api:${CI_COMMIT_SHORT_SHA}
environment:
DEBUG: "False"
SECRET_KEY: ${DJANGO_SECRET_KEY}
ALLOWED_HOSTS: api.trinity.jostud.com,localhost
DB_HOST: db
DB_NAME: trinity
DB_USER: trinity
DB_PASSWORD: ${POSTGRES_PASSWORD}
networks:
- trinity
- internal
deploy:
replicas: 2
update_config:
order: start-first
failure_action: rollback
web:
image: ${CI_REGISTRY_IMAGE}/web:${CI_COMMIT_SHORT_SHA}
environment:
API_URL: https://api.trinity.jostud.com
networks:
- trinity
deploy:
replicas: 2
update_config:
order: start-first
failure_action: rollback
docs:
image: ${CI_REGISTRY_IMAGE}/docs:${CI_COMMIT_SHORT_SHA}
networks:
- trinity
deploy:
update_config:
order: start-first
failure_action: rollback
volumes:
pgdata:
networks:
trinity:
external: true
internal:What to notice:
image: …:${CI_COMMIT_SHORT_SHA}: prod runs exactly the image built and tested by the pipeline for this commit.deploy.update_config:start-firstandrollback, the heart of the zero-downtime deployment.- No
build:norrestart:: Swarm does not build images, and it restarts stopped tasks itself.
Why two machines?
At first, dev and prod were to share a single server. They were split (chore(ansible): split servers into dev and prod machines):
- a dev deployment that fills memory or disk does not affect prod;
- each machine has its own Traefik routes and its own deploy runner;
- a
devrunner cannot, even by mistake, deploy to prod.
Dev does not use the registry images
Dev tests the code, but not the exact image that will go to prod: that one is only built with Dockerfile.prod, in api:build and web:build. See Limits.