Skip to content

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 ​

devprod
MachineVM trinity-devVM trinity-prod
Branchdevelopmain
Addressesdev.trinity.jostud.com, api.dev.trinity.jostud.comtrinity.jostud.com, api.trinity.jostud.com, docs.trinity.jostud.com
Fileinfra/docker-compose.dev.ymlinfra/docker-compose.prod.yml
Started withdocker compose up -d --builddocker stack deploy (Swarm)
Imagesbuilt on the server from Dockerfile.devpulled from the registry: api:<sha>, web:<sha>
API serverDjango's runserverGunicorn, 3 workers
Web servernext devnode server.js (standalone build)
Django DEBUGTrue: detailed error pagesFalse
User in the containersrootnon-root (appuser, nextjs)
Docker health checknoyes
Updatecontainers recreated: short outagenew version started before the old one stops: zero downtime
Failed updatethe job fails, the containers stay brokenautomatic rollback to the previous version
Migrationsapi service command: migrate then runserverentrypoint.sh: migrate then collectstatic
Auto restartrestart: unless-stoppedhandled by Swarm
DatabasePostgreSQL 17, pgdata volumePostgreSQL 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 answer pg_isready.
  • ${POSTGRES_PASSWORD}, ${DJANGO_SECRET_KEY}: filled from GitLab's CI/CD variables at job time. See Secrets and access.
  • container_name: sets trinity_api and trinity_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-first and rollback, the heart of the zero-downtime deployment.
  • No build: nor restart:: 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 dev runner 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.