Docker Compose is a powerful tool for defining and running multi-container Docker applications. It uses a YAML file (compose.yaml
) to configure application services, networks, volumes, and more. This file allows developers to declaratively describe infrastructure and dependencies, making it easier to manage complex environments.
Whether you’re building a local dev stack or deploying to production, Compose simplifies orchestration and keeps your configuration readable and version-controlled.
compose.yaml
-
for each item.env
filesservices:
service_name:
image: image_name:tag
build:
context: .
dockerfile: Dockerfile
ports:
- "host_port:container_port"
volumes:
- ./host_path:/container_path
environment:
- VAR_NAME=value
depends_on:
- other_service
networks:
- custom_network
networks:
custom_network:
driver: bridge
volumes:
custom_volume:
Each service defines a container.
services:
web:
image: nginx:latest
build:
context: ./app
dockerfile: Dockerfile
command: ["nginx", "-g", "daemon off;"]
container_name: custom_name
ports:
- "8080:80"
expose:
- "80"
environment:
- DEBUG=true
env_file:
- .env
volumes:
- ./data:/data
restart: always
depends_on:
- db
networks:
- frontend
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost"]
interval: 30s
timeout: 10s
retries: 5
build:
context: ./dir
dockerfile: Dockerfile
args:
build_arg: value
target: build-stage
build:
Tells Compose how to build the image. context:
is the directory containing the Dockerfile and source code.dockerfile:
lets you specify a custom Dockerfile name or path.volumes:
data_volume:
driver: local
driver_opts:
type: none
device: /path/on/host
o: bind
volumes:
- data_volume:/app/data
- ./local:/container/path
volumes:
Mounts host directories or named volumes into the container. ./src:/app/src
mounts the local src
folder into the container at /app/src
.networks:
frontend:
driver: bridge
backend:
driver: overlay
networks:
Connects the service to one or more custom networks. Enables service discovery and isolation.services:
app:
networks:
- frontend
- backend
ports:
- "3000:3000"
ports:
Maps container ports to host ports. Format is "host:container"
. Useful for exposing services to your local machine.secrets:
db_password:
file: ./db_password.txt
services:
db:
secrets:
- db_password
configs:
my_config:
file: ./config.txt
services:
app:
configs:
- source: my_config
target: /etc/config.txt
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost"]
interval: 30s
timeout: 10s
retries: 3
start_period: 5s
healthcheck:
Defines how Docker checks if the container is healthy. test:
is the command to runinterval:
how often to run the checktimeout:
how long to wait for a responseretries:
how many failures before marking unhealthyrestart: no # Never restart
restart: always # Always restart
restart: on-failure # Restart on failure
restart: unless-stopped
environment:
- VAR1=value1
- VAR2=value2
env_file:
- .env
environment:
Sets environment variables inside the container. Useful for configuration.env_file:
Loads environment variables from a file. Keeps secrets and config separate from the Compose file. command: npm start
command:
Overrides the default command defined in the Dockerfile. Useful for customizing container behavior. depends_on:
- db
depends_on:
Specifies service startup order. In Compose, this does not wait for the service to be “ready”—just started.docker compose down # Stop and remove containers, networks, volumes
docker compose down -v # Also remove named volumes
Command | Description |
---|---|
docker compose up | Start services |
docker compose up -d | Start in detached mode |
docker compose down | Stop and remove services |
docker compose build | Build images |
docker compose ps | List containers |
docker compose logs | View logs |
docker compose exec <service> <cmd> | Run command in container |
docker compose config | Validate and view config |
Docker Compose supports GPU access via the device_requests
field (Compose v3.8+ and Docker 19.03+).
services:
gpu-app:
image: nvidia/cuda:11.0-base
deploy:
resources:
reservations:
devices:
- driver: nvidia
count: all
capabilities: [gpu]
services:
igpu-app:
image: intel/openvino
devices:
- /dev/dri:/dev/dri
🔧 Make sure your host has the necessary drivers and runtime (e.g., NVIDIA Container Toolkit or Intel Media SDK).
Feature | Docker Compose (compose.yaml ) | Docker Swarm (stack.yml ) |
---|---|---|
restart | ✅ Supported | ❌ Not supported |
depends_on | ✅ Supported | ❌ Not supported |
deploy | ❌ Ignored | ✅ Required for replicas |
build | ✅ Supported | ❌ Not supported |
volumes (bind) | ✅ Supported | ✅ Supported |
configs /secrets | ❌ Ignored | ✅ Supported |
healthcheck | ✅ Supported | ✅ Supported |
🧠 Tip: Use
compose.yaml
for local development andstack.yml
for Swarm deployments.
Profiles allow conditional inclusion of services based on the active profile. This is useful for separating dev/test/staging environments.
services:
web:
image: nginx
profiles:
- default
debug:
image: busybox
command: top
profiles:
- debug
docker compose --profile debug up
profiles
key are always included.Docker Compose and Docker Swarm both use YAML files to define services, but they serve different purposes and support different features. Compose is optimized for local development and testing, while Swarm is designed for production-grade orchestration across clusters.
Mode | Use Case |
---|---|
Compose | Local development, testing |
Swarm | Cluster deployment, scaling |
Feature | Compose (compose.yaml ) | Swarm (stack.yml ) |
---|---|---|
build: | ✅ Supported | ❌ Ignored |
restart: | ✅ Supported | ❌ Ignored |
depends_on: | ✅ Supported | ❌ Ignored |
deploy: | ❌ Ignored | ✅ Required for scaling, placement |
configs: | ❌ Ignored | ✅ Supported |
secrets: | ❌ Ignored | ✅ Supported |
healthcheck: | ✅ Supported | ✅ Supported |
volumes: (bind) | ✅ Supported | ✅ Supported |
networks: | ✅ Supported | ✅ Supported |
profiles: | ✅ Supported (v3.9+) | ❌ Not supported |
These features are useful for local development but are ignored in Swarm:
build:
services:
app:
build:
context: .
dockerfile: Dockerfile
restart:
restart: unless-stopped
deploy.restart_policy
instead.depends_on:
depends_on:
- db
These features are exclusive to Swarm and ignored by Compose:
deploy:
services:
app:
deploy:
replicas: 3
placement:
constraints:
- node.role == manager
restart_policy:
condition: on-failure
configs:
and secrets:
configs:
app_config:
file: ./config.yml
secrets:
db_password:
file: ./password.txt
placement:
(inside deploy
)placement:
constraints:
- node.labels.env == production
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost"]
interval: 30s
timeout: 10s
retries: 3
Type | Compose | Swarm |
---|---|---|
Bind mount | ✅ | ✅ |
Named volume | ✅ | ✅ |
External volume | ✅ | ✅ |
Volume driver options | ✅ | ✅ |
Swarm requires external volumes to be pre-created on all nodes.
Feature Category | Compose | Swarm |
---|---|---|
Local builds | ✅ | ❌ |
Cluster scaling | ❌ | ✅ |
Secrets/configs | ❌ | ✅ |
Profiles | ✅ | ❌ |
Restart policies | ✅ | ✅ (via deploy ) |
Service dependencies | ✅ | ❌ |
🧭 Tip: Use
compose.yaml
for development andstack.yml
for Swarm. You can split your configuration into multiple files or use tools likekompose
to convert Kubernetes manifests.