services:
web:
image: nginx:alpine
ports:
- "8080:80"Use this for browser access from the host.
Compose patterns for ports, networks, volumes, secrets, configs, and runtime controls.
Publish ports and design inter-service connectivity.
services:
web:
image: nginx:alpine
ports:
- "8080:80"Use this for browser access from the host.
services:
db:
image: postgres:16
ports:
- "127.0.0.1:5432:5432"Useful for local databases not meant for LAN exposure.
services:
web:
image: nginx:alpine
networks: ["frontend"]
api:
image: myapi:latest
networks: ["frontend", "backend"]
networks:
frontend: {}
backend: {}Great for separating public and private service paths.
services:
api:
image: myapi:latest
networks:
backend:
aliases:
- internal-api
networks:
backend: {}Useful when multiple apps expect a specific hostname.
docker compose port web 80Useful when ports were assigned dynamically.
Persist data and mount source code or configs.
services:
db:
image: postgres:16
volumes:
- pgdata:/var/lib/postgresql/data
volumes:
pgdata: {}Preferred over bind mounts for durable container data.
services:
app:
image: node:20
working_dir: /app
volumes:
- ./:/appVery common for live-reload development flows.
services:
nginx:
image: nginx:alpine
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf:roGood for config files that should not be modified in-place.
services:
app:
image: myapp:latest
tmpfs:
- /tmpUseful for ephemeral scratch data.
docker compose down -vUse with caution on any stateful stack.
Inject files and sensitive values into services.
services:
api:
image: myapi:latest
secrets:
- app_key
secrets:
app_key:
file: ./secrets/app_key.txtUseful for credentials and tokens you do not want inline in YAML.
services:
nginx:
image: nginx:alpine
configs:
- source: nginx_conf
target: /etc/nginx/conf.d/default.conf
configs:
nginx_conf:
file: ./nginx/default.confHelpful when treating config as a managed resource.
Control runtime behavior and limits.
services:
api:
image: myapi:latest
restart: unless-stoppedA common default for long-running local or small-host services.
services:
app:
image: myapp:latest
ulimits:
nofile:
soft: 65535
hard: 65535Useful for proxies, databases, and event-driven services.
services:
app:
image: myapp:latest
extra_hosts:
- "host.docker.internal:host-gateway"Helpful for connecting back to services on the Docker host.