DEV Community

SOVANNARO
SOVANNARO

Posted on • Edited on

πŸš€ Create Your First Service and Scale It Locally

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

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

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

Check the tasks (containers) for the service:

docker service ps web
Enter fullscreen mode Exit fullscreen mode

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

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

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

And if you want to leave swarm mode:

docker swarm leave --force
Enter fullscreen mode Exit fullscreen mode

πŸŽ‰ 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.

Top comments (0)