Skip to content

Compose

Compose describes an application made of several containers (services, volumes, networks) in a single compose.yaml.

services:
db:
image: postgres:17
environment:
POSTGRES_PASSWORD: secret
volumes:
- pgdata:/var/lib/postgresql/data
restart: unless-stopped
api:
build: .
ports:
- "8000:8000"
environment:
DB_HOST: db
depends_on:
- db
restart: unless-stopped
volumes:
pgdata:

Compose creates a network for the project automatically, so api reaches the database at host db.

Terminal window
docker compose up -d # create and start everything in the background
docker compose up -d --build # rebuild images first
docker compose ps # state of the services
docker compose logs -f api # follow the logs of one service
docker compose exec api sh # shell inside a service
docker compose restart api
docker compose stop # stop without removing
docker compose down # stop and remove containers and the network
docker compose down -v # … and the volumes too (deletes the data!)
docker compose pull # download newer versions of the images
docker compose config # show the final configuration, with variables resolved

With Podman, use podman compose with the same subcommands. It delegates to podman-compose or docker-compose, whichever is installed.

Compose reads a .env file next to compose.yaml:

.env
POSTGRES_PASSWORD=secret
environment:
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}

Keep .env out of Git if it holds passwords.