This content originally appeared on DEV Community and was authored by SOVANNARO
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>
Copy the join command Docker gives you.
On worker nodes:
docker swarm join --token <TOKEN> <MANAGER-IP>:2377
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]
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
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
Or see where containers are running:
docker service ps myapp_web
Step 4: Scale Like a Boss
Want to handle more traffic?
Just scale your services!
docker service scale myapp_web=5
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
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
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!
This content originally appeared on DEV Community and was authored by SOVANNARO