DevOps Docker Compose
Docker Compose is a tool that starts several containers together from one file. A real application rarely runs alone. A website needs a web server, a database, and often a cache. Compose describes all of them in a single YAML file and starts everything with one command.
Why Compose Helps
Imagine planning a dinner party. You could call the caterer, the florist, and the musician one by one. A party planner handles every call from a single checklist. Compose acts as that planner for containers. You describe the whole setup once, and Compose creates every piece in the right way.
How Compose Fits Together
docker-compose.yml
|
v
+---------------------------------+
| Private Network |
| |
| [ web ] -----> [ db ] |
| port 8080 port 5432 |
| | |
| +---------> [ cache ] |
+---------------------------------+
|
Volume: db-data (saved on disk)
Compose creates a private network for the project. Each container reaches the others by service name, so the web container connects to a database at the address db.
A Complete Example
services:
web:
image: nginx:1.27
ports:
- "8080:80"
depends_on:
- db
db:
image: postgres:16
environment:
POSTGRES_PASSWORD: example
volumes:
- db-data:/var/lib/postgresql/data
volumes:
db-data:Understanding Each Part
Services
A service is one container definition. The file above defines two services named web and db.
Image
The image key names the package that the container starts from. Compose downloads the image if it is missing.
Ports
The value 8080:80 maps port 8080 on your computer to port 80 inside the container. Open a browser at localhost:8080 to see the web server.
Volumes
A container loses its data when removed. A volume stores data outside the container, so the database keeps its records after a restart.
Environment Variables
The environment key passes settings into the container. For real projects, store passwords in a separate env file and keep that file out of Git.
Depends On
The depends_on key controls start order. Compose starts the database container before the web container.
Everyday Commands
| Command | Purpose |
|---|---|
| docker compose up -d | Start all services in the background |
| docker compose ps | List running services |
| docker compose logs -f web | Follow the logs of the web service |
| docker compose stop | Pause the services |
| docker compose down | Remove containers and the network |
Building Your Own Image
Replace the image key with a build key to create an image from a Dockerfile in the project folder.
services:
app:
build: .
ports:
- "3000:3000"Networks in Compose
Compose places all services on one default network. Custom networks split services into groups and limit who can talk to whom. A database can sit on a private network that only the backend reaches, while the web server sits on a public one.
[ web ] --- frontend network --- [ api ] --- backend network --- [ db ] (web cannot reach db directly)
services:
web:
image: nginx:1.27
networks: [frontend]
api:
image: my-api:1.0
networks: [frontend, backend]
db:
image: postgres:16
networks: [backend]
networks:
frontend:
backend:Health Checks
A health check runs a small command inside the container to test whether the service works. The depends_on key can wait for a healthy status instead of a plain start.
services:
db:
image: postgres:16
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 10s
retries: 5
web:
image: nginx:1.27
depends_on:
db:
condition: service_healthyEnv Files and Override Files
An env file named .env stores variables that Compose reads automatically. Use the file for ports, image versions, and passwords, and keep it out of Git.
services:
web:
image: nginx:${NGINX_VERSION}
ports:
- "${WEB_PORT}:80"Override files change a base file for another environment. Compose loads docker-compose.yml first and merges docker-compose.override.yml on top. A second command picks a different file set.
docker compose -f docker-compose.yml -f docker-compose.prod.yml up -dScaling and Restart Policies
Run several copies of one service with the scale option, such as docker compose up -d --scale web=3. A restart policy tells Docker what to do after a crash. The value unless-stopped restarts a container after failures and after a server reboot, unless an engineer stopped it on purpose.
Profiles
A profile marks optional services. A debug tool or a test database can carry the profile name debug. Compose starts that service only when you pass --profile debug, so everyday runs stay light.
Key Points
- Compose defines multi-container applications in one YAML file.
- Services talk to each other by name on a private network.
- Volumes protect data from container removal.
- One command starts or stops the entire stack.
