Docker Compose · Lesson 8 of 10 · 3 min read
Everyday commands and workflows
docker compose up -d # create/start everything in the background
docker compose up -d --build # rebuild images first
docker compose up -d --force-recreate api
docker compose up -d --no-deps api # recreate api without touching dependencies
docker compose ps # status and health
docker compose logs -f --tail 100 api
docker compose exec api sh # shell in the RUNNING container
docker compose run --rm api npm test # one-off command in a NEW container
docker compose stop / start / restart
docker compose down # remove containers and networks (keeps volumes)
docker compose down -v --remove-orphans # also volumes and containers of removed services
docker compose pull && docker compose up -d # update to newer images
docker compose config # resolved, validated configuration
docker compose top / images / port api 3000
docker compose watch # sync/rebuild on file changes (develop.watch)
execuses an existing container;runcreates a new one (and starts dependencies unless--no-deps). Userun --rmfor migrations and tests.upis idempotent: it only recreates containers whose configuration or image changed.- Orphans are containers from services you've removed from the file;
--remove-orphanscleans them.
Scaling
docker compose up -d --scale worker=3
Fails with "port is already allocated" if the service publishes a fixed host port — three containers can't all bind host port 8080. Either don't publish a host port for scaled services, publish a range, or put a reverse proxy (Nginx, Traefik) in front that reaches replicas by service name. Also remove any fixed container_name, which prevents scaling.
Compose in CI integration tests
export COMPOSE_PROJECT_NAME=ci-$BUILD_NUMBER
docker compose -f compose.yaml -f compose.ci.yaml up -d --wait
docker compose run --rm tests
status=$?
docker compose logs > compose-logs.txt # keep logs as an artifact
docker compose down -v --remove-orphans
exit $status
Unique project names keep parallel builds from colliding; always tear down (in a post/finally step) so agents don't fill up.