Welcome back! Now that youโve mastered Docker Compose and Swarm individually, itโs time to combine their powers. In this episode, weโll explore how to deploy multi-container applications across multiple hosts using Compose with Swarm.
๐น Why Integrate Compose with Swarm?
- Compose simplifies service definitions.
- Swarm provides orchestration, scaling, and high availability.
- Together, they allow easy deployment of complex applications across clusters.
๐น Preparing Compose for Swarm
- Docker Compose v3 supports Swarm mode.
- Define services, networks, and volumes as usual.
- Use the
deploysection for Swarm-specific settings:
version: '3.8'
services:
web:
image: nginx:latest
ports:
- "80:80"
deploy:
replicas: 3
update_config:
parallelism: 1
delay: 10s
restart_policy:
condition: on-failure
๐น Deploying Compose to Swarm
docker stack deploy -c docker-compose.yml mystack
-
docker stack deployinterprets Compose files in Swarm mode. - Services defined in Compose are now Swarm services.
Check running services:
docker stack services mystack
๐น Scaling & Updates
- Scale a service:
docker service scale mystack_web=5
- Perform rolling updates by updating the image in
docker-compose.ymland redeploying:
docker stack deploy -c docker-compose.yml mystack
๐น Networking & Secrets
- Overlay networks defined in Compose are created automatically across Swarm nodes.
- Secrets and configs can also be defined in Compose:
secrets:
db_password:
file: ./db_password.txt
services:
db:
image: postgres:latest
secrets:
- db_password
๐น Best Practices
- Use Compose v3+ for Swarm compatibility.
- Define replicas, update_config, and restart_policy under deploy.
- Use overlay networks for multi-host communication.
- Manage secrets and configs via Compose.
- Test on a small Swarm cluster before production deployment.
๐น Hands-On Challenge
- Create a multi-container Compose app (web + db + cache).
- Add deploy settings for Swarm (replicas, update_config).
- Deploy it using
docker stack deployacross multiple nodes. - Test scaling, rolling updates, and secret usage.
โ Next Episode: Episode 25 โ Docker Troubleshooting & Debugging: Common Issues & Fixes โ learn to identify and solve real-world container problems efficiently.
Top comments (0)