Geavanceerd
Zoeken inHow-to-gidsen
Zo zoek je
  • Typ de taak in je eigen woorden (poort vinden, firewall openen, schijf vol) of plak een foutmelding tussen aanhalingstekens.
  • Kies "Snel antwoord" om alleen de commando's te zien, of een gebied om in één vakgebied te blijven.
  • "twee woorden" vindt precies die zin.
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken
Probeer:

How-to 28Containers · Docker

How to run an app with Docker Compose

A real app is more than one container: the app, a database, maybe a cache. Docker Compose describes all of them in one file — images, ports, environment, volumes, health checks and start order — and starts them with one command. This how-to writes a compose.yaml for an app with PostgreSQL, keeps the password out of the file, shows the daily commands, and how to update without losing data.

  • 5 Lessen
  • 7 Commando's

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

$ als gewone gebruiker# als root (of met sudo)

28.1

The quick answer

compose.yaml
services:
  api:
    image: registry.example.com/shop/api:1.4.2
    ports:
      - "127.0.0.1:3000:3000"
    environment:
      DATABASE_URL: postgres://shop:${DB_PASSWORD:?set DB_PASSWORD in .env}@db:5432/shop
    depends_on:
      db:
        condition: service_healthy
    restart: unless-stopped
 
  db:
    image: postgres:17
    environment:
      POSTGRES_USER: shop
      POSTGRES_DB: shop
      POSTGRES_PASSWORD: ${DB_PASSWORD:?set DB_PASSWORD in .env}
    volumes:
      - db-data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U shop -d shop"]
      interval: 10s
      retries: 5
    restart: unless-stopped
 
volumes:
  db-data:
docker compose up -d
docker compose ps

28.2

Step by step

  1. **Keep secrets in .env** (next to compose.yaml, never in Git):
.env
DB_PASSWORD=<a long random password>
chmod 600 .env
echo .env >> .gitignore
openssl rand -base64 24

${DB_PASSWORD:?…} stops with a clear error if the variable is missing.

  1. Check the file before you start (shows the final config with variables filled in):
docker compose config --quiet && echo OK
  1. Start, look, follow:
docker compose up -d
docker compose ps
NAME           IMAGE                                 STATUS                   PORTS
shop-api-1     registry.example.com/shop/api:1.4.2   Up 20 seconds            127.0.0.1:3000->3000/tcp
shop-db-1      postgres:17                           Up 31 seconds (healthy)
docker compose logs -f --tail 50 api
docker compose exec db psql -U shop -d shop -c 'select 1'

Services find each other by service name (db:5432) on the project network — the database needs no published port.

  1. Daily commands:
CommandDoes
docker compose up -dCreate/start everything; recreate what changed.
docker compose pull && docker compose up -dUpdate to the images in the file.
docker compose up -d --buildRebuild local images (with build: in the file).
docker compose restart apiRestart one service.
docker compose stop / startStop / start without removing.
docker compose downRemove containers and network — volumes stay.
docker compose down -vAlso remove the volumes — deletes the database.
  1. Update the app: change the tag in compose.yaml (1.4.2 → 1.4.3), then:
docker compose pull api
docker compose up -d api

Only api is recreated; db and its volume stay as they are.

  1. Back up the database before big changes:
docker compose exec -T db pg_dump -U shop shop | gzip > shop-$(date +%F).sql.gz

28.3

Check that it worked

docker compose ps shows all services Up, the database (healthy); curl -sI http://127.0.0.1:3000 answers; after docker compose down and up -d the data is still there (it lives in the db-data volume).

28.4

Common problems

ProblemCauseFix
docker-compose: command not foundCompose v2 is docker compose (a plugin).Install docker-compose-plugin; use docker compose.
App starts before the database is readydepends_on without a health check.condition: service_healthy + a healthcheck on the db.
required variable DB_PASSWORD is missingNo .env, or run from another folder.Create .env next to compose.yaml; run in that folder (or -f).
Database is empty after a restartdown -v, or the volume path is wrong.Never -v on real data; check volumes: path for the image.
Changed password has no effect on PostgreSQLThe password is only used at the first start of an empty volume.Change it inside the DB (ALTER USER), or start with a new volume.
port is already allocatedAnother service uses the host port.Change the host part of ports: or stop the other service.

28.5

Go further

Alle commando's in dit hoofdstuk