DEV Community

SOVANNARO
SOVANNARO

Posted on • Edited on

πŸš€ Create a Multi-Service Multi-Node Web App with Docker Swarm

Hey there, fellow dev! πŸ‘‹
Have you ever dreamed of running a real-world web app that can scale across multiple servers, with different services working together like a perfect orchestra? Well, today is your lucky day!

In this post, we’ll show you how to build a multi-service, multi-node web app using Docker Swarm. You’ll learn how to:

βœ… Set up multiple nodes (servers)
βœ… Create multiple services (like web and database)
βœ… Let them talk to each other
βœ… Scale like a pro!

Ready? Let’s dive in!


πŸ’‘ What You'll Build

We’ll create a mini web app with two main services:

  • Frontend (web): A simple Nginx server serving a website
  • Backend (database): A Redis server (our example backend)

These services will run across multiple nodes in a Docker Swarm cluster β€” meaning they can survive crashes, scale easily, and balance traffic smartly!


🧱 Step 1: Create a Swarm Cluster

First, you need at least 3 machines (real or virtual). You can use:

  • 3 VMs (VirtualBox, EC2, DigitalOcean, etc.)
  • Or 3 Docker containers with Swarm support

Example:

Let’s say we have:

  • manager-node
  • worker-node-1
  • worker-node-2

On the manager node:

docker swarm init --advertise-addr <MANAGER-IP>
Enter fullscreen mode Exit fullscreen mode

Copy the join command Docker gives you.

On worker nodes:

docker swarm join --token <TOKEN> <MANAGER-IP>:2377
Enter fullscreen mode Exit fullscreen mode

πŸŽ‰ Boom! You’ve got a 3-node Swarm cluster!


πŸ› οΈ Step 2: Create a Docker Compose File

Now, let’s define our multi-service app.

Create a file called docker-compose.yml:

version: "3.9"

services:
  web:
    image: nginx
    ports:
      - "8080:80"
    deploy:
      replicas: 3
      placement:
        constraints: [node.role == worker]

  redis:
    image: redis
    deploy:
      replicas: 1
      placement:
        constraints: [node.role == worker]
Enter fullscreen mode Exit fullscreen mode

What this does:

  • Spins up 3 Nginx web servers
  • Runs 1 Redis server
  • Deploys them only on worker nodes

πŸš€ Step 3: Deploy the Stack

Copy the docker-compose.yml to your manager node.

Then deploy it using:

docker stack deploy -c docker-compose.yml myapp
Enter fullscreen mode Exit fullscreen mode

Docker Swarm will magically:

  • Schedule your containers across available nodes
  • Connect them with an overlay network
  • Handle load balancing and failure recovery

You can check the status with:

docker stack services myapp
Enter fullscreen mode Exit fullscreen mode

Or see where containers are running:

docker service ps myapp_web
Enter fullscreen mode Exit fullscreen mode

πŸ”„ Step 4: Scale Like a Boss

Want to handle more traffic?

Just scale your services!

docker service scale myapp_web=5
Enter fullscreen mode Exit fullscreen mode

Boom πŸ’₯ β€” now you have 5 web servers working together, across multiple machines!


πŸ§ͺ Bonus: Test It!

Visit your server’s IP at port 8080:

http://<ANY_NODE_IP>:8080
Enter fullscreen mode Exit fullscreen mode

You should see the Nginx welcome page. Docker Swarm will balance traffic to all replicas for you.


🧼 Clean Up

To remove the stack:

docker stack rm myapp
Enter fullscreen mode Exit fullscreen mode

🎁 Final Thoughts

You just built a multi-service, multi-node web app like a pro! πŸ§‘β€πŸ’»πŸ’ͺ
With Docker Swarm, it’s easy to:

  • Create scalable apps
  • Run across multiple servers
  • Keep everything connected and balanced

Whether you're building a blog, an e-commerce app, or a microservice monster β€” this is your foundation.


❀️ Enjoyed this?

If this helped you, share it with your dev friends and don’t forget to explore more Docker magic!

Top comments (0)