Docker Compose: Managing Multi-Container Applications
Last updated June 15, 2023
DevOps

Guide Parts
Docker Compose: Managing Multi-Container Applications
Docker Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application's services, networks, and volumes, then create and start all the services with a single command.
Why Use Docker Compose?
Most real-world applications consist of multiple interconnected services. For example, a web application might need:
- A frontend web server
- A backend API
- A database
- A cache service
- Message queues
Managing these containers individually becomes cumbersome. Docker Compose solves this by allowing you to:
- Define all services in a single file
- Start all services with one command
- Create isolated environments for each project
The docker-compose.yml File
The core of Docker Compose is the docker-compose.yml
file. Here's a basic example:
version: "3"
services:
web:
image: nginx:latest
ports:
- "8080:80"
volumes:
- ./website:/usr/share/nginx/html
depends_on:
- api
api:
build: ./api
ports:
- "3000:3000"
environment:
- DB_HOST=db
- DB_USER=postgres
- DB_PASSWORD=example
depends_on:
- db
db:
image: postgres:14
volumes:
- postgres_data:/var/lib/postgresql/data
environment:
- POSTGRES_PASSWORD=example
- POSTGRES_USER=postgres
- POSTGRES_DB=myapp
volumes:
postgres_data:
This configuration defines three services: a web server, an API, and a database.
Essential Docker Compose Commands
Command | Description |
---|---|
docker-compose up | Create and start all services |
docker-compose up -d | Start in detached mode (background) |
docker-compose down | Stop and remove containers, networks |
docker-compose down -v | Also remove volumes |
docker-compose ps | List running services |
docker-compose logs | View output from all services |
docker-compose logs SERVICE | View output from specific service |
docker-compose build | Build or rebuild services |
docker-compose exec SERVICE COMMAND | Run a command in a service |
docker-compose restart | Restart all services |
A Real-World Example: MERN Stack Application
Here's a practical example of a MERN (MongoDB, Express, React, Node.js) stack application:
version: "3"
services:
frontend:
build: ./client
ports:
- "3000:3000"
volumes:
- ./client:/app
- /app/node_modules
depends_on:
- backend
environment:
- REACT_APP_API_URL=http://localhost:5000/api
backend:
build: ./server
ports:
- "5000:5000"
volumes:
- ./server:/app
- /app/node_modules
depends_on:
- mongodb
environment:
- MONGO_URI=mongodb://mongodb:27017/myapp
- PORT=5000
mongodb:
image: mongo:latest
ports:
- "27017:27017"
volumes:
- mongo_data:/data/db
environment:
- MONGO_INITDB_DATABASE=myapp
volumes:
mongo_data:
Best Practices for Docker Compose
- Use environment variables: Store sensitive information in
.env
files - Define restart policies: Add
restart: always
orrestart: unless-stopped
for production - Name your volumes: Use named volumes instead of host paths for persistence
- Use networks: Create custom networks for better isolation
- Version control: Keep your compose files in version control
- Health checks: Add health checks for critical services
Scaling Services
Docker Compose allows you to run multiple instances of a service:
docker-compose up -d --scale api=3
This command starts three instances of the API service, useful for load balancing.
In the next section, we'll explore how to deploy Docker applications to production environments.
Enjoyed the read? Help us spread the word — say something nice!
Guide Parts
Enjoyed the read? Help us spread the word — say something nice!
Guide Parts