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)