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.
Publishes the service only to the local machine.
services:
db:
image: postgres:16
ports:
- "127.0.0.1:5432:5432"Useful for local databases not meant for LAN exposure.
Defines multiple named networks for segmentation.
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.
Adds alternate DNS names for a service on a network.
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.
Stores database data in a managed named volume.
services:
db:
image: postgres:16
volumes:
- pgdata:/var/lib/postgresql/data
volumes:
pgdata: {}Preferred over bind mounts for durable container data.
Mounts the current directory into the container.
services:
app:
image: node:20
working_dir: /app
volumes:
- ./:/appVery common for live-reload development flows.
Mounts a host file as read-only in the container.
services:
nginx:
image: nginx:alpine
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf:roGood for config files that should not be modified in-place.
Creates an in-memory temporary filesystem for the service.
services:
app:
image: myapp:latest
tmpfs:
- /tmpUseful for ephemeral scratch data.
Tears down the project and deletes named volumes created for it.
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.
Provides a structured way to mount a config artifact.
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.
Restarts the service unless it is explicitly stopped.
services:
api:
image: myapi:latest
restart: unless-stoppedA common default for long-running local or small-host services.
Sets per-container ulimits for file handles and similar resources.
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.