Docker Series: Episode 24 β€” Docker Compose + Swarm Integration: Multi-Host Deployments 🌍



This content originally appeared on DEV Community and was authored by Yash Sonawane

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 deploy section 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 deploy interprets 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.yml and 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

  1. Use Compose v3+ for Swarm compatibility.
  2. Define replicas, update_config, and restart_policy under deploy.
  3. Use overlay networks for multi-host communication.
  4. Manage secrets and configs via Compose.
  5. Test on a small Swarm cluster before production deployment.

🔹 Hands-On Challenge

  1. Create a multi-container Compose app (web + db + cache).
  2. Add deploy settings for Swarm (replicas, update_config).
  3. Deploy it using docker stack deploy across multiple nodes.
  4. 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.


This content originally appeared on DEV Community and was authored by Yash Sonawane