🚀 Create a Multi-Service Multi-Node Web App with Docker Swarm
SOVANNARO

SOVANNARO @sovannaro

About: Passionate developer specializing in JavaScript, TypeScript, React, Vue, and Angular. I thrive on solving problems and building innovative solutions that make a difference in the modern world.

Location:
Phnom Penh, Cambodia
Joined:
Dec 1, 2021

🚀 Create a Multi-Service Multi-Node Web App with Docker Swarm

Publish Date: Jun 26
1 0

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!

Comments 0 total

    Add comment