If you are starting to work with Docker, and you have a big application to deploy, you have to learn about Swarm. It allows you to use multiple machines together as an orchestra.

From my experience, you can do it the hard way, or you can use Docker Swarm.

Why Docker?

As part of my internship at the Nearsoft Academy, I contributed to an Open Source project. After some research, I chose to contribute to Docker repositories.

Docker is a really innovative tool to create, deploy, and run applications. It will become an indispensable tool for you as a developer.

The tool I will concentrate in is Docker Swarm. It has plenty of documentation. If you are a Docker enthusiast, you’ll love its features, like I did.

Docker Swarm

In Docker-talk, a swarm is a cluster of machines running and working together,

  • A node refers to a each machine in the Swarm
  • Any node can be a manager or a worker
  • A leader is the top-most manager

The advantage of a cluster is to load-balance. Since the request will be assigned randomly to the nodes and not a single machine. The managers can pass docker commands to workers, and deploy everything as a service to achieve load balance.

Get Started

To use swarm you must do the following,

  • Have Docker installed
  • Have an image to deploy
  • Have a docker-compose.yml file to create the services
  • Be on a Docker Machine

How to Work with Docker Swarm

The three key things you’ll want to do is to Create, Initialize, and Run the Swarm.

1. Create the Swarm

You can use physical or virtual machines to incorporate them in a Swarm. You will need the IP address of each machine and a token generated by Swarm. If you use Virtual Machines, you can use,

And get a list of your machines using,

2. Initialize the Swarm

Once you figure which machine will be the leader, you can initialize the Swarm Mode. At the same time assign that machine as the manager and leader of the swarm.

  • As leader, the machine will have admin rights in the swarm. As manager, it will have rights over the other managers and workers.
  • Workers cannot pass commands, they can only receive them.

To initialize as leader, run the next command,

If you want to check how this is initialized, look in the [Docker CLI] repository. It will show you how Docker commands work, including the commands for Docker Swarm.

On the init.go file, the Docker CLI code has the method runInit. It invokes swarmInit to initialize the Swarm.

One of the main things I like about Swarm is that the CLI guides you by telling which commands to use next.

For example, in this case, it first tells you how to initialize the swarm using the current node as a manager.

Then, it tells you how to initialize the rest of the machines as workers or managers, using the Out method of the CLI class.

This is what you’ll see in your console,

To add a worker or manager use,

Use this approach to join machines with the IP and a provided token in the Docker CLI.

On the join.go file, the code has the runJoin method, which uses again the swarmInit method, shown next,

Note that runJoin has a similar functionality to the runInit method. The difference is that with runJoin, you specify if the node joined will be a manager of a worker.

3. Deploy the Swarm

Once you have the swarm setup, you can deploy it by using,

Note the new element, called a stack. A stack is a group of interrelated services. This can be the your whole application or a fragment of it.

You use a Docker-compose file to configure the services. It deploys the image of the application you would want to use.

If you use any of the IP addresses for the swarm nodes, you can access the application from a web browser.


  • Docker Swarm allows you to load-balancing by creating a cluster of multiple machines running Docker.
  • By assigning nodes as managers, you can pass Docker commands to other machines.
  • The leader manager can alter the swarm state.
  • Docker Swarm, with the help of the Docker CLI, can guide you showing what commands to use, and what each parameter should contain.

If you have any questions, contact me at [email protected].