Dit deel is nog niet vertaald. Je ziet de Engelse tekst.
$ als gewone gebruiker# als root (of met sudo)
The quick answer
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:Step by step
- **Keep secrets in
.env** (next tocompose.yaml, never in Git):
DB_PASSWORD=<a long random password>${DB_PASSWORD:?…} stops with a clear error if the variable is missing.
- Check the file before you start (shows the final config with variables filled in):
- 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.
- Daily commands:
| Command | Does |
|---|---|
docker compose up -d | Create/start everything; recreate what changed. |
docker compose pull && docker compose up -d | Update to the images in the file. |
docker compose up -d --build | Rebuild local images (with build: in the file). |
docker compose restart api | Restart one service. |
docker compose stop / start | Stop / start without removing. |
docker compose down | Remove containers and network — volumes stay. |
docker compose down -v | Also remove the volumes — deletes the database. |
- Update the app: change the tag in
compose.yaml(1.4.2→1.4.3), then:
Only api is recreated; db and its volume stay as they are.
- Back up the database before big changes:
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).
Common problems
| Problem | Cause | Fix |
|---|---|---|
docker-compose: command not found | Compose v2 is docker compose (a plugin). | Install docker-compose-plugin; use docker compose. |
| App starts before the database is ready | depends_on without a health check. | condition: service_healthy + a healthcheck on the db. |
required variable DB_PASSWORD is missing | No .env, or run from another folder. | Create .env next to compose.yaml; run in that folder (or -f). |
| Database is empty after a restart | down -v, or the volume path is wrong. | Never -v on real data; check volumes: path for the image. |
| Changed password has no effect on PostgreSQL | The 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 allocated | Another service uses the host port. | Change the host part of ports: or stop the other service. |
Go further
- Command pages: docker, openssl.
- Next how-tos: Run a container and publish a port, See the logs of a container, Write a small and safe Dockerfile, Back up and restore PostgreSQL, Debug the network between containers, Back up a Docker volume, Route containers with Traefik labels.
- Learn it: Docker — The Compose file, Docker — Compose commands, Docker — Health checks, Docker — Overrides.
- Full guides: Install Docker, PostgreSQL, Portainer.
- Practise: Lab: run a web app, a database and a cache with Docker Compose.
1Does docker compose down delete the database data?
No — volumes stay. Only docker compose down -v deletes them.
2How does the api container reach the database?
By the service name (db:5432) on the project's network; no published port is needed.