How to run multi-service applications with Docker Compose?
· Category: Docker
Short answer
Use docker compose up to start all services defined in your Compose file. Add -d to run in detached mode and --build to rebuild images before starting containers.
Steps
- Ensure
docker-compose.ymlis in the current directory. - Run
docker compose up -dto start all services in the background. - View logs with
docker compose logs -f. - Stop services with
docker compose down.
Example
docker compose up -d
Rebuild and start:
docker compose up -d --build
Start a specific service:
docker compose up -d web
Tips
- Use
--remove-orphansto clean up containers for services no longer defined. - Use
--scale web=3to run multiple instances of a service. - Use
docker compose psto see the status of all services.
Common issues
- Services may fail if they depend on a database that is not yet ready; use health checks.
- Port conflicts occur when multiple Compose projects try to bind the same host port.
- Stale containers from previous runs can cause unexpected behavior; run
docker compose downfirst.