DEV Community

SOVANNARO
SOVANNARO

Posted 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)