Docker, Compose and Swarm
Three levels of Docker tooling are used, from simplest to most complete.
| Tool | What it manages | Where in the project |
|---|---|---|
| one image, one container | the Dockerfiles, the *:build jobs | |
| several containers described in one file | the dev environment, Traefik | |
| services that update with zero downtime | the prod environment |
The Dockerfiles
Each application has two recipes:
| API dev | API prod | Web dev | Web prod | |
|---|---|---|---|---|
| Base | python:3.11-slim | python:3.11-slim | node:22-alpine | node:22-alpine |
| Stages | 1 | 2 | 1 | 3 |
| Server | runserver | Gunicorn | next dev | node server.js |
| User | root | appuser | root | nextjs |
| Health check | — | /health, 30 s | — | /api/health, 10 s |
The details are in Configuration and startup (API) and Configuration, tests, build (web).
Why multi-stage images?
The build stage needs heavy tools (all npm dependencies, the install tooling). The final image only keeps the result: it is lighter and has a smaller attack surface.
Building an image in the pipeline
The api:build and web:build jobs run in a container. To run docker build, they start a service:
web:build:
stage: build
image: docker:27.5.1-cli
services:
- name: docker:27.5.1-dind
alias: docker
variables:
DOCKER_HOST: tcp://docker:2376
DOCKER_TLS_CERTDIR: /certs
DOCKER_TLS_VERIFY: "1"
DOCKER_CERT_PATH: "$DOCKER_TLS_CERTDIR/client"
before_script:
# Identifiants fournis automatiquement par GitLab, jamais écrits dans le repo.
- echo "$CI_REGISTRY_PASSWORD" | docker login "$CI_REGISTRY" --username "$CI_REGISTRY_USER" --password-stdin
script:
- docker build --pull --file web/Dockerfile.prod --tag "$CI_REGISTRY_IMAGE/web:$CI_COMMIT_SHORT_SHA" web
- docker push "$CI_REGISTRY_IMAGE/web:$CI_COMMIT_SHORT_SHA"
rules:
- if: '$CI_COMMIT_BRANCH == "develop" || $CI_COMMIT_BRANCH == "main"'services: docker:27.5.1-dindstarts a Docker daemon next to the job.DOCKER_HOST+ TLS: the job's client talks to this daemon over an encrypted channel.docker loginwith the job's temporary token, thenbuildandpush.--pulldownloads the base image again, to get its security fixes.
Compose
A Compose file describes services, their networks and their volumes. docker compose up -d creates them all; run again, it only recreates what changed. Used for the dev environment and for Traefik.
Swarm
Swarm turns Docker into a small orchestrator. docker swarm init is run by Ansible on both servers, but only prod uses it.
| Concept | Here |
|---|---|
| Stack | everything described by docker-compose.prod.yml, named trinity |
| Service | trinity_api, trinity_web, trinity_db |
| Task | one container of a service |
update_config | how tasks are replaced during an update |
docker stack deploy -c infra/docker-compose.prod.yml trinity # create or update
docker service ls # services and their image
docker service ps trinity_web # task history
docker service logs trinity_api # logsWhat Swarm brings, and why it was chosen: Zero-downtime deployment.
Volumes
| Volume | Contains | Survives |
|---|---|---|
pgdata | PostgreSQL's files | a restart, a redeployment |
Deleting the volume deletes all the data.
docker compose down -v and docker volume rm
The -v option deletes volumes. On a server, never use -v without having backed up the database.