Run the stack
docker compose up -d # start everything in the backgrounddocker compose up -d --build # rebuild images firstdocker compose up -d api # one service (and what it depends on)docker compose ps # status, ports, healthdocker compose stop # stop, keep containersdocker compose down # stop and remove containers, networksdocker compose down -v # ...and named volumes (data!)docker compose restart apiLook inside
docker compose logs -f api # follow one servicedocker compose logs --tail 50 --since 10mdocker compose exec api sh # shell in a running servicedocker compose run --rm api npm test # one-off containerdocker compose top # processes per servicedocker compose config # final file, variables filled indocker compose config --services # service namesA compose.yaml
services: db: image: postgres:17 environment: POSTGRES_PASSWORD: ${DB_PASSWORD:?set DB_PASSWORD in .env} volumes: - db-data:/var/lib/postgresql/data healthcheck: test: ["CMD-SHELL", "pg_isready -U postgres"] interval: 5s retries: 10
api: build: ./api env_file: .env ports: - "127.0.0.1:8000:8000" depends_on: db: condition: service_healthy restart: unless-stopped
volumes: db-data:The Docker Compose Generator writes one for common stacks; the docker run ⇄ Compose Converter turns your docker run commands into one.
Variables
| In the file | Means |
|---|---|
${VAR} |
Value from the shell or .env |
${VAR:-default} |
Default if unset or empty |
${VAR:?message} |
Stop with this message if unset or empty |
$$ |
A literal $ |
.env next to compose.yaml fills the file; env_file: passes variables into the container. Two different things.
Several files, profiles, projects
docker compose -f compose.yaml -f compose.prod.yaml up -d # later files overridedocker compose --profile tools up -d # also start services with profiles: [tools]docker compose -p shop up -d # project name (default: folder name)docker compose up -d --scale worker=3docker compose watch # sync code changes (develop: watch)compose.override.yaml is read automatically, after compose.yaml.
Images and cleanup
docker compose pull # newer imagesdocker compose build --no-cache apidocker compose imagesdocker compose rm -f # stopped service containersdocker image prune -a # unused imagesCommon errors
'compose' is not a docker commandAdditional property … is not allowedport is already allocatedcontainer name is already in use- A container can’t reach
localhost non-overlapping IPv4 address poolmapping values are not allowed