DEV Community

Yash Sonawane
Yash Sonawane

Posted on

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

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
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή Deploying Compose to Swarm

docker stack deploy -c docker-compose.yml mystack
Enter fullscreen mode Exit fullscreen mode
  • docker stack deploy interprets Compose files in Swarm mode.
  • Services defined in Compose are now Swarm services.

Check running services:

docker stack services mystack
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή Scaling & Updates

  • Scale a service:
docker service scale mystack_web=5
Enter fullscreen mode Exit fullscreen mode
  • Perform rolling updates by updating the image in docker-compose.yml and redeploying:
docker stack deploy -c docker-compose.yml mystack
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή 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
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή 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.

Top comments (0)