Skip to content

Docker, Compose and Swarm ​

Three levels of Docker tooling are used, from simplest to most complete.

ToolWhat it managesWhere in the project
one image, one containerthe Dockerfiles, the *:build jobs
several containers described in one filethe dev environment, Traefik
services that update with zero downtimethe prod environment

The Dockerfiles ​

Each application has two recipes:

API devAPI prodWeb devWeb prod
Basepython:3.11-slimpython:3.11-slimnode:22-alpinenode:22-alpine
Stages1213
ServerrunserverGunicornnext devnode server.js
Userrootappuserrootnextjs
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:

yaml
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-dind starts a Docker daemon next to the job.
  • DOCKER_HOST + TLS: the job's client talks to this daemon over an encrypted channel.
  • docker login with the job's temporary token, then build and push.
  • --pull downloads 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.

ConceptHere
Stackeverything described by docker-compose.prod.yml, named trinity
Servicetrinity_api, trinity_web, trinity_db
Taskone container of a service
update_confighow tasks are replaced during an update
sh
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                                # logs

What Swarm brings, and why it was chosen: Zero-downtime deployment.

Volumes ​

VolumeContainsSurvives
pgdataPostgreSQL's filesa 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.