Working with Docker Stacks
1. Creating Stack Compose File
| Field | Description | Example |
|---|---|---|
| version | Version of the Compose file format | '3.8' |
| services | Defines the services that make up the stack | |
| volumes | Defines volumes to be used by services | volumes: data_volume |
| networks | Defines networks to be used by services | networks: frontend |
Example: Basic Stack Compose File
version: '3.8'
services:
web:
image: nginx
ports:
- "80:80"
database:
image: mysql:8.0
environment:
MYSQL_ROOT_PASSWORD: password
volumes:
data_volume:
networks:
frontend:
2. Deploying Stacks
| Option | Values | Description |
|---|---|---|
| -c, --compose-file | docker-compose.yml |
Path to the Compose file |
| --detach | true |
Run in detached mode (default: true) |
| --resolve-image | always |
Resolve image tags to digests (default: always) |
Note: The stack must be deployed to a Docker Swarm cluster. Use
docker swarm init to initialize the swarm if needed.
Warning: Ensure that all services in the Compose file have unique names to avoid conflicts during deployment.
3. Listing Stacks
| Command | Description | Options |
|---|---|---|
| docker stack ls | List all running stacks | -f, --filter Filter output--format Format output using Go template |
4. Listing Stack Services
| Command | Description | Options |
|---|---|---|
| docker stack services <stack_name> | List all services in a stack | --format to format output, --filter to filter results |
Note: The
docker stack services command lists all services defined in the specified stack, including their status, replicas, and other relevant details.
5. Viewing Stack Tasks
| Task ID | Service Name | Status | Created |
|---|---|---|---|
| task-123abc | web |
RUNNING | 2026-01-15 10:30 |
| task-456def | db |
STOPPED | 2026-01-14 09:15 |
Note: The
docker stack tasks command lists all tasks in a stack, showing their current status and associated service.
6. Configuring Stack Networks
| Network Property | Description | Default Value | Notes |
|---|---|---|---|
| driver | Driver to use for the network | overlay |
Must be overlay for swarm mode |
| attachable | Allow external containers to connect | false |
Enables non-service containers to join |
| internal | Restrict external access | false |
Prevents access from outside the swarm |
| ipv6 | Enable IPv6 support | false |
Requires Docker daemon config |
Example: Create an Internal Stack Network
docker network create \
--driver overlay \
--internal \
--subnet 10.0.9.0/24 \
stack-net
Note: Internal networks do not have access to the host network.
Example: Attach Service to Custom Network
version: '3.8'
services:
web:
image: nginx
networks:
- stack-net
networks:
stack-net:
external: true
7. Configuring Stack Volumes
| Volume Type | Description | Usage |
|---|---|---|
| bind | Mounts a host directory as a volume | docker stack deploy -c docker-compose.yml myapp |
| volume | Creates a named volume managed by Docker | docker volume create myvolume |
| tmpfs | Mounts a temporary filesystem in memory | -t tmpfs -v /tmp |
Example: Bind Mount Volume
version: '3.8'
services:
web:
image: nginx
volumes:
- /host/data:/usr/share/nginx/html
Example: Named Volume
version: '3.8'
services:
db:
image: postgres
volumes:
- db_data:/var/lib/postgresql/data
volumes:
db_data:
Note: Volume configuration in stacks is managed via
docker-compose.yml files, not directly through CLI commands.
8. Using Stack Secrets
| Property | Description | Example |
|---|---|---|
| secret.name | Name of the secret to be used in the stack | db_password |
| secret.source | Source of the secret (file or literal) | file or literal |
| secret.file | Path to the file containing the secret (when source is file) | /path/to/secret.txt |
| secret.literal | Literal value of the secret (when source is literal) | my_secret_value |
Example: Define a secret in a stack compose file
version: "3.8"
services:
web:
image: nginx
secrets:
- db_password
secrets:
db_password:
file: ./db_password.txt
Example: Using a literal secret
version: "3.8"
services:
web:
image: nginx
secrets:
- api_key
secrets:
api_key:
external: true
Note: Secrets are mounted as files in the service's filesystem, typically under
/run/secrets.
9. Using Stack Configs
| Config Name | Source | Description | Usage |
|---|---|---|---|
| config.name | docker stack config |
Name of the configuration | Used to reference the config in services |
| config.data | docker config create |
Content of the configuration | Can be a file or string |
| config.file | docker config create |
Path to file for config data | Used when reading from local files |
Example: Use Config in Stack
version: '3.8'
services:
web:
image: nginx
configs:
- my-config
configs:
my-config:
external: true
Note: Stack configs are global and can be shared across services in a stack.
10. Updating Stack Deployments
| Command | Description | Notes |
|---|---|---|
| docker stack deploy | Update a stack with new configuration | Use --compose-file to specify updated compose file |
Note: Changes to services or configs will be applied during deployment
Warning: Rolling updates may cause downtime depending on service configuration
| Option | Description | Default |
|---|---|---|
| --compose-file | Path to updated compose file | None |
| --with-registry-auth | Use registry auth from local config | false |
11. Monitoring Stack Health
| Command | Description | Example |
|---|---|---|
| docker stack ps | Lists tasks in a stack | docker stack ps myapp |
| docker stack ls | Lists all stacks | docker stack ls |
| docker service logs | Displays logs for a service | docker service logs myapp_web |
Example: Check stack task status
docker stack ps myapp
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
abc123 myapp_web.1 nginx:latest node-1 Running Running 2 hours ago
Note: Use
docker stack ps --format json for structured output.Warning: If a task is in
Failed state, check logs with docker service logs.12. Removing Stacks
| Command | Description | Flags |
|---|---|---|
| docker stack rm | Removes a stack from the swarm | --force - Force removal even if stack is in use |
Note: This command removes all services, networks, and volumes associated with the stack.
Warning: Removing a stack will delete all resources created by it. Ensure data is backed up before removal.