Docker
Docker Compose
Define and run multi-container apps with compose.
By EZ4Code Team
composeorchestrationyaml
Code
# docker-compose.yml
services:
web:
image: nginx:alpine
ports:
- "8080:80"
volumes:
- ./html:/usr/share/nginx/html
depends_on:
- api
api:
build: ./api
environment:
- DB_HOST=db
depends_on:
- db
db:
image: postgres:16
environment:
POSTGRES_PASSWORD: secret
volumes:
- pgdata:/var/lib/postgresql/data
volumes:
pgdata:
# Commands
# docker compose up -d # start in background
# docker compose logs -f web # follow logs
# docker compose down # stop and removeExplanation
Compose lets you declare multiple services, their dependencies, networks, and volumes in a single YAML file. depends_on controls startup order but does not wait for readiness; use healthchecks for that. docker compose up brings the stack up, and down tears it down.
More Docker Snippets
Run a Container
Run, list, stop, and remove Docker containers.
Writing a Dockerfile
Build an image from a Dockerfile with multi-instruction layers.
Image Management
Pull, list, tag, push, and prune images.
Volumes & Bind Mounts
Persist data with named volumes, anonymous volumes, and bind mounts.
Networking
Create networks, attach containers, and expose ports.
Multi-Stage Build
Build artifacts in one stage and copy them into a slim final image.