Skip to main content

Docker Compose explained

Compose describes one or more services in a YAML file. The following is a standalone tutorial example, not a copy of a production stack.

services:
demo-web:
image: nginx:stable-alpine
container_name: demo-web
restart: unless-stopped
ports:
- "8080:80"
volumes:
- ./site:/usr/share/nginx/html:ro
KeyMeaning
servicesContainer definitions
imageImage to pull
container_nameHuman-friendly name
restartRestart policy
portsHost-to-container port mapping
volumesBind mount or persistent storage
:roRead-only mount

Run the example​

mkdir -p ~/compose-demo/site && cd ~/compose-demo
printf '<h1>It works!</h1>\n' > site/index.html
# Save the YAML above as compose.yml
docker compose config --quiet
docker compose up -d
docker compose ps
curl -I http://127.0.0.1:8080

Stop it with docker compose down. For production, pin tested image versions or digests rather than blindly updating every container.