Skip to content

Docker Compose

First PublishedByAtif Alam

Docker Compose lets you define and run multi-container applications in a single place. You describe services, networks, and volumes in a Compose file (usually docker-compose.yml) and use docker compose up to start everything.

services:
web:
image: nginx:alpine
ports:
- "8080:80"
depends_on:
- api
api:
build: ./api
environment:
- DB_HOST=db
- DB_PORT=5432
depends_on:
- db
db:
image: postgres:16-alpine
environment:
POSTGRES_USER: app
POSTGRES_PASSWORD: secret
volumes:
- dbdata:/var/lib/postgresql/data
volumes:
dbdata:
FieldPurpose
servicesEach key is a service name (e.g. web, api, db).
imageUse a pre-built image (e.g. nginx:alpine).
buildBuild from a Dockerfile (path or context + dockerfile).
portsPublish ports "host:container".
environmentEnvironment variables for the container.
volumesMount named volumes or bind mounts.
depends_onStart order (does not wait for health).
networksAttach services to user-defined networks.
Terminal window
# Start all services (build if needed, foreground)
docker compose up
# Start in background
docker compose up -d
# Build and start
docker compose up -d --build
# Stop and remove containers (volumes persist by default)
docker compose down
# Stop and remove containers and named volumes
docker compose down -v
# List running services
docker compose ps
# View logs (all services or one)
docker compose logs -f
docker compose logs -f api

By default Compose creates a single network per project and attaches all services to it. Containers can reach each other by service name (e.g. db, api). To use a custom network:

services:
web:
networks:
- front
api:
networks:
- front
- back
db:
networks:
- back
networks:
front: {}
back: {}
  • Define services, volumes, and networks in docker-compose.yml; use depends_on for start order.
  • docker compose up -d starts the stack; docker compose down stops and removes containers. Use docker compose logs and docker compose ps to inspect.