Docker Compose — Orchestrate Multi-Container Applications
Practical guide to Docker Compose — start services, follow logs, scale containers and structure compose files, with examples for every command.
Docker Compose describes a complete multi-container application in a single YAML file – services, networks and volumes included. Instead of starting each container by hand with long docker run commands, you bring the whole stack up with one docker compose up and tear it down again with docker compose down. Since Compose v2 the tool ships as the docker compose subcommand built right into the Docker CLI – the old hyphenated docker-compose is considered legacy. This guide walks you through the commands you reach for daily, from starting and scaling individual services through following logs to the building blocks of a compose file.
Basic Commands
docker compose up — Create and start all services defined in the compose file.
docker compose updocker compose up -d — Start services in detached mode (background).
docker compose up -ddocker compose down — Stop and remove containers, networks, and default volumes.
docker compose downdocker compose down -v — Stop services and remove named volumes declared in the compose file.
docker compose down -vdocker compose down --rmi all — Stop services and remove all images used by the services.
docker compose down --rmi alldocker compose start — Start existing (already created) containers.
docker compose startdocker compose stop — Stop running services without removing containers.
docker compose stopdocker compose restart — Restart all services.
docker compose restartdocker compose restart <service> — Restart a specific service.
docker compose restart nginxService Management
docker compose ps — List containers and their status for the current project.
docker compose psdocker compose ps -a — List all containers including stopped ones.
docker compose ps -adocker compose up -d <service> — Start only a specific service (and its dependencies).
docker compose up -d webdocker compose stop <service> — Stop a specific service.
docker compose stop workerdocker compose rm <service> — Remove stopped service containers.
docker compose rm workerdocker compose up -d --scale <service>=<n> — Scale a service to n instances.
docker compose up -d --scale worker=3docker compose up -d --no-deps <service> — Recreate a service without restarting its dependencies.
docker compose up -d --no-deps webLogs & Monitoring
docker compose logs — View logs from all services.
docker compose logsdocker compose logs -f — Follow (stream) logs from all services in real time.
docker compose logs -fdocker compose logs <service> — View logs from a specific service.
docker compose logs nginxdocker compose logs -f --tail <n> <service> — Follow logs showing only the last n lines.
docker compose logs -f --tail 50 webdocker compose logs --since <time> — Show logs since a specific time or duration.
docker compose logs --since 30mdocker compose top — Display the running processes in each service container.
docker compose topExecuting Commands
docker compose exec <service> <command> — Execute a command in a running service container.
docker compose exec web bashdocker compose exec -T <service> <command> — Execute a command without a pseudo-TTY (for scripting).
docker compose exec -T db mysqldump mydb > backup.sqldocker compose exec -u <user> <service> <command> — Execute a command as a specific user.
docker compose exec -u root web bashdocker compose run <service> <command> — Run a one-off command in a new container (not the running one).
docker compose run web npm installdocker compose run --rm <service> <command> — Run a one-off command and remove the container afterwards.
docker compose run --rm web php artisan migrateBuilding & Pulling
docker compose build — Build or rebuild all service images.
docker compose builddocker compose build <service> — Build a specific service image.
docker compose build webdocker compose build --no-cache — Build without using the layer cache (clean rebuild).
docker compose build --no-cachedocker compose pull — Pull the latest images for all services.
docker compose pulldocker compose up -d --build — Rebuild images before starting services.
docker compose up -d --builddocker compose push — Push built service images to a registry.
docker compose pushConfiguration & Debugging
docker compose config — Validate and display the resolved compose file.
docker compose configdocker compose config --services — List all service names defined in the compose file.
docker compose config --servicesdocker compose config --volumes — List all volume names defined in the compose file.
docker compose config --volumesdocker compose -f <file> up -d — Use a specific compose file instead of the default.
docker compose -f docker-compose.prod.yml up -ddocker compose -f docker-compose.yml -f docker-compose.override.yml up -d — Merge multiple compose files (later files override earlier ones).
docker compose -f docker-compose.yml -f docker-compose.dev.yml up -ddocker compose --env-file <file> up -d — Specify an environment file for variable substitution.
docker compose --env-file .env.production up -ddocker compose --profile <name> up -d — Start services tagged with a specific profile.
docker compose --profile debug up -dVolumes & Networks
docker compose cp <service>:<src> <dest> — Copy files from a service container to the local filesystem.
docker compose cp web:/var/log/app.log ./logs/docker compose cp <src> <service>:<dest> — Copy files from the local filesystem into a service container.
docker compose cp ./config.json web:/app/config.jsondocker volume ls — List all Docker volumes (including those created by compose).
docker volume lsdocker volume inspect <project>_<volume> — Inspect a specific compose volume (prefixed with project name).
docker volume inspect myapp_db-datadocker network ls — List all Docker networks (including those created by compose).
docker network lsdocker network inspect <project>_<network> — Inspect a compose network and see connected containers.
docker network inspect myapp_defaultCommon Compose File Snippets
ports: — Basic service with port mapping.
services:
web:
image: nginx:alpine
ports:
- "8080:80"build: — Service built from local Dockerfile with bind mount.
services:
app:
build:
context: .
dockerfile: Dockerfile
volumes:
- .:/appdepends_on: — Wait for a dependency's health check to pass before starting.
services:
web:
depends_on:
db:
condition: service_healthyhealthcheck: — Define a health check for a service.
services:
web:
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost"]
interval: 30s
timeout: 10s
retries: 3volumes: — Define a named volume for persistent data storage.
services:
db:
volumes:
- db-data:/var/lib/mysql
volumes:
db-data:
driver: localenv_file: — Load environment variables from a file into the service.
services:
web:
env_file:
- .env
- .env.local Conclusion
Docker Compose is the link between a single container and full-blown orchestration: for development, testing and smaller deployments it covers nearly every need. As your stack grows, it pays to explore profiles, layered compose files and health checks – keeping a single YAML file as a reproducible, version-controlled image of your entire application.
Further Reading
- Docker Compose – official documentation – reference and guides
- Compose file reference – every key and option in detail
- Docker – Wikipedia – background and ecosystem