Chapter 03

The whole stack with Compose

One file, one command, and a new developer has a running database, API and front end in about ninety seconds. This is the single highest-value hour in the whole course.

⏱ 18 min readLevel: intermediateHands-on

What we are wiring up

Browser reaching the web container, which proxies /api to the api container, which connects to the db container with a named volume
One network, three services, one volume. The database port is not published.

docker-compose.yml

docker-compose.yml
services:

  db:
    image: postgres:16-alpine
    environment:
      POSTGRES_DB: oms
      POSTGRES_USER: oms_app
      POSTGRES_PASSWORD: ${DB_PASSWORD:?set DB_PASSWORD in .env}
    volumes:
      - db-data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U oms_app -d oms"]
      interval: 5s
      timeout: 3s
      retries: 10
      start_period: 10s
    restart: unless-stopped
    # no ports: — nothing outside the compose network needs it

  api:
    build:
      context: ./api
      target: runtime
    environment:
      SPRING_PROFILES_ACTIVE: docker
      SPRING_DATASOURCE_URL: jdbc:postgresql://db:5432/oms
      SPRING_DATASOURCE_USERNAME: oms_app
      SPRING_DATASOURCE_PASSWORD: ${DB_PASSWORD}
      JWT_PRIVATE_KEY_PATH: /run/secrets/jwt-private.pem
      JWT_PUBLIC_KEY_PATH:  /run/secrets/jwt-public.pem
    volumes:
      - ./secrets:/run/secrets:ro
    depends_on:
      db:
        condition: service_healthy      # wait for ready, not just started
    healthcheck:
      test: ["CMD", "curl", "-fsS", "http://localhost:8080/actuator/health/readiness"]
      interval: 10s
      timeout: 3s
      retries: 6
      start_period: 45s
    restart: unless-stopped

  web:
    build:
      context: ./web
      target: runtime
    ports:
      - "4200:8080"                     # the only published port
    depends_on:
      api:
        condition: service_healthy
    restart: unless-stopped

volumes:
  db-data:
condition: service_healthy is the important line

Plain depends_on only waits for the container to start. PostgreSQL accepts a TCP connection well before it is ready to serve, so the API starts, Flyway fails to connect, and the stack dies on a cold boot. Gating on the health check removes an entire class of flaky start-ups — and every retry loop people write to work around it.

One origin, no CORS

web/nginx.conf — add to the server block from chapter 2
location /api/ {
    proxy_pass         http://api:8080/api/;
    proxy_set_header   Host              $host;
    proxy_set_header   X-Real-IP         $remote_addr;
    proxy_set_header   X-Forwarded-For   $proxy_add_x_forwarded_for;
    proxy_set_header   X-Forwarded-Proto $scheme;
    proxy_read_timeout 60s;
}

The browser only ever talks to localhost:4200. Angular calls /api/orders as a relative URL, Nginx forwards it to the API container, and the CORS configuration from chapter 4 of the project becomes unnecessary in this environment.

The .env file

.env.example — committed. .env itself is gitignored.
DB_PASSWORD=change-me-locally
COMPOSE_PROJECT_NAME=oms
${DB_PASSWORD:?…} fails loudly

The :? form aborts compose up with your message when the variable is missing. Without it the password becomes an empty string and you debug a confusing authentication error instead of reading "set DB_PASSWORD in .env".

A development override

docker-compose.override.yml — applied automatically, never in CI
services:
  db:
    ports:
      - "5432:5432"        # so you can attach psql or a GUI client

  api:
    environment:
      SPRING_PROFILES_ACTIVE: docker,dev
      LOGGING_LEVEL_COM_BITSNIPPETS: DEBUG
    ports:
      - "8080:8080"
      - "5005:5005"        # remote debugger
    command: >
      java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005
           org.springframework.boot.loader.launch.JarLauncher

docker compose up merges base + override locally. CI runs docker compose -f docker-compose.yml up and gets the production shape.

Working with it

the commands worth memorising
docker compose up -d --build          # start everything, rebuilding images
docker compose ps                     # status and health of each service
docker compose logs -f api            # follow one service
docker compose exec db psql -U oms_app -d oms
docker compose restart api
docker compose up -d --no-deps --build api    # rebuild just the API

docker compose down                   # stop and remove containers, KEEP the volume
docker compose down -v                # ...and delete the database. Destructive.
down -v deletes your database

The -v removes named volumes, which is exactly what you want to reset seed data and exactly what you do not want at 5pm on a Friday. Muscle memory is real — type it deliberately.

Integration tests against the real stack

docker-compose.test.yml
services:
  db:
    environment:
      POSTGRES_PASSWORD: test
    tmpfs:
      - /var/lib/postgresql/data     # RAM disk: fast, and disposable by design

  e2e:
    image: mcr.microsoft.com/playwright:v1.47.0-jammy
    working_dir: /work
    volumes:
      - ./web:/work
    environment:
      BASE_URL: http://web:8080
    depends_on:
      web:
        condition: service_healthy
    command: npx playwright test
one command in CI
docker compose -f docker-compose.yml -f docker-compose.test.yml \
  up --build --abort-on-container-exit --exit-code-from e2e

--exit-code-from e2e makes the whole command return Playwright's exit code, so the pipeline fails when a test fails. This is the job chapter 4 drops into GitHub Actions.

When it does not come up

SymptomUsual cause
API exits immediatelycompose logs api — usually a Flyway or datasource failure
Connection refused to dbUsing localhost instead of the service name db
Service stuck "starting"Health-check command missing from the image (no curl)
Old code keeps runningNo --build; Compose reused the cached image
Port already allocatedAnother Compose project or a local PostgreSQL on 5432
Migration checksum mismatchAn applied migration was edited — down -v to reset locally

Why does the db service publish no ports in the base file?

Services on the same Compose network reach each other by name on their real ports. Publishing is only for traffic entering from outside. The dev override adds 5432 for convenience, and that override never runs in CI or production.