Let’s try Swarm Mode in action — on your own machine!
You don’t need multiple servers to get started. One computer is enough to test Swarm locally! 💻
🔹 Step 1: Initialize Swarm Mode
Open your terminal and run:
docker swarm init
This command turns your current Docker host into a Swarm Manager.
✅ Done! You now have a one-node Swarm cluster running locally.
🔹 Step 2: Create a Simple Service
Let’s launch a simple nginx
web service with 3 replicas:
docker service create --name web --replicas 3 -p 8080:80 nginx
What this does:
- 🚀 Launches the Nginx image as a service
- 🔁 Runs 3 replicas of it
- 🌐 Maps port 8080 (your machine) to port 80 (inside the container)
Now visit http://localhost:8080 to see it in action!
🔹 Step 3: Check Your Service
See your service in the swarm:
docker service ls
Check the tasks (containers) for the service:
docker service ps web
You should see 3 tasks running — Docker is managing everything for you!
🔹 Step 4: Scale It Up! (or Down 😎)
Want more replicas? No problem:
docker service scale web=5
Docker will automatically add 2 more replicas and balance them across nodes (in your case, on the same machine).
Want to reduce it back?
docker service scale web=2
Swarm handles the rest — no restarts, no headaches.
🔹 Step 5: Tear It Down
When you’re done experimenting, clean up with:
docker service rm web
And if you want to leave swarm mode:
docker swarm leave --force
🎉 That’s It!
You just deployed and scaled your first Docker service using Swarm Mode — locally!
It’s a powerful yet super approachable way to start learning container orchestration without diving straight into complex tools like Kubernetes.
Tidak ada komentar:
Posting Komentar