Stage 4 · Provision
Docker Compose
Compose Operations
Operate stacks with docker compose up, down, logs, ps, exec, run, pull, and build.
Lifecycle Commands
Docker Compose provides commands to manage the entire lifecycle of your application stack. These commands operate on all services defined in your compose.yaml file.
| Command | Purpose |
|---|---|
| docker compose up | Create and start all services |
| docker compose down | Stop and remove containers, networks |
| docker compose stop | Stop services without removing them |
| docker compose start | Start stopped services |
| docker compose restart | Restart services |
| docker compose rm | Remove stopped service containers |
# Start all services in foreground
docker compose up
# Start in detached mode
docker compose up -d
# Stop and remove everything
docker compose down
# Stop and remove including volumes
docker compose down -v
# Stop without removing (preserves state)
docker compose stop
# Start stopped services
docker compose startup creates networks, volumes, and containers. down removes them. stop preserves containers for faster restart. -v removes named volumes defined in the compose file.
Building Images
# Build all services
docker compose build
# Build a specific service
docker compose build api
# Build without cache
docker compose build --no-cache api
# Pull latest base images
docker compose pull
# Build and start
docker compose up --build
# Build with specific target
docker compose build --target production apidocker compose build builds images for all services that have a build key. --build in up forces a rebuild before starting. pull downloads the latest versions of base images.
Viewing Logs
# Follow logs from all services
docker compose logs -f
# Follow logs from a specific service
docker compose logs -f api
# Show last 100 lines
docker compose logs --tail 100
# Show logs since 5 minutes ago
docker compose logs --since 5m
# Show timestamps
docker compose logs -t apidocker compose logs aggregates logs from all services. -f follows new output (like tail -f). --tail limits the number of lines. You can filter by service name.
Exec and Run
# Execute a command in a running service
docker compose exec api sh
# Execute as root
docker compose exec -u root api sh
# Run a one-off command (creates a new container)
docker compose run api python manage.py migrate
# Run and remove the container after
docker compose run --rm api python manage.py test
# Pass environment variables
docker compose run -e DEBUG=true api python script.pyexec runs in an existing container. run creates a new container for one-off tasks. Use --rm with run to clean up after the command completes.
Scaling Services
# Scale a service to 3 instances
docker compose up -d --scale api=3
# Scale multiple services
docker compose up -d --scale api=3 --scale worker=2
# Check running instances
docker compose psScaling creates multiple containers for the same service. Each instance gets a unique name (e.g., app-api-1, app-api-2). Load balancing between instances requires an external tool like Traefik.
Useful Flags
| Flag | Purpose |
|---|---|
| -d | Detached mode (background) |
| --build | Force rebuild before starting |
| -f FILE | Specify compose file |
| -p NAME | Set project name |
| --profile NAME | Activate a profile |
| --remove-orphans | Remove containers for services not in the file |
| --force-recreate | Recreate containers even if unchanged |
# Full rebuild and restart
docker compose up -d --build --force-recreate
# Clean start with orphan removal
docker compose up -d --remove-orphans
# Specific file and project
docker compose -f compose.prod.yaml -p production up -d
# Dry run (show what would happen)
docker compose up --dry-runThese flags modify how Compose executes commands. Combine them as needed for your workflow. --dry-run is useful for debugging Compose configuration.
When starting fresh, docker compose down -v removes containers, networks, AND volumes. This ensures no stale data interferes. Be careful — this deletes all persistent data.
Mark this lesson complete to store local progress and unlock a cleaner resume path the next time you visit.