Here’s a Docker Compose cheat sheet to help you quickly reference common commands and configurations when working with Docker Compose for managing multi-container applications:
Basic Commands:
- Start Containers:
docker-compose up
- Start Containers in the Background:
docker-compose up -d
- Stop Containers:
docker-compose down
- Build or Rebuild Services:
docker-compose build
- View Logs:
docker-compose logs
- List Containers:
docker-compose ps
Configuration File:
- Create a Docker Compose File:
touch docker-compose.yml
- Edit the Docker Compose File: Use a text editor like
nano
orvim
. - Specify Services: Define services, networks, and volumes in YAML format.
Services:
- Define Services:
services:
web:
image: nginx:latest
ports:
- "80:80"
- Scale Services:
docker-compose up --scale web=3
Environment Variables:
- Set Environment Variables:
services:
app:
image: myapp:latest
environment:
- DEBUG=true
Networking:
- Create Custom Networks:
networks:
mynetwork:
- Connect Services to Networks:
services:
app:
networks:
- mynetwork
Volumes:
- Mount Volumes:
services:
db:
volumes:
- /path/on/host:/path/in/container
- Named Volumes:
services:
app:
volumes:
- datavolume:/app/data
volumes:
datavolume:
Docker Compose Commands:
- Specify Docker Compose File:
docker-compose -f docker-compose.prod.yml up
- Build and Start Specific Service:
docker-compose up -d web
Docker Compose Override:
- Use Override Files:
docker-compose -f docker-compose.yml -f docker-compose.override.yml up
Interacting with Containers:
- Execute a Command in a Running Container:
docker-compose exec <service_name> <command>
- Open a Shell in a Running Container:
docker-compose exec <service_name> sh
Clean Up:
- Remove Stopped Containers:
docker-compose rm
- Remove Volumes:
docker-compose down -v
Healthchecks:
- Define Healthchecks:
services:
web:
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost"]
interval: 1m
timeout: 10s
retries: 3
This cheat sheet covers fundamental Docker Compose commands and configurations. For more details, refer to the official Docker Compose documentation. Customize these commands based on your specific application requirements.