Docker Swarm

Docker Swarm

In this tutorial, we are going to discuss on Docker Swarm quick introduction. With Docker swarm you could combine multiple Docker machines together into a single cluster.

Docker swarm will take care of distributing your services or your application instances into separate hosts for high availability and for load balancing across different systems and hardware.

Docker Swarm
Setup swarm

To set up a Docker swarm, you must first have hosts or multiple hosts with Docker installed on them.

Then you must designate one host to be the manager or the master or it’s the swarm manager as it is called and others as slaves or workers.

Once you’re done with that run the docker swarm init command on the swarm manager and that will initialize the swarm manager.

$ docker swarm init --advertise-addr 192.168.99.121
Swarm initialized: current node (bvz81updecsj6wjz393c09vti) is now a manager.

To add a worker to this swarm, run the following command:
docker swarm join \     --token SWMTKN-1-3pu6hszjas19xyp7ghgosyx9k8atbfcr8p2is99znpy26u2lkl-1awxwuwd3z9j1z3puu7rcgdbx \     172.17.0.2:2377

To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions. 

The output will also provide the command to be run on the workers. So copy the command and run it on the worker nodes to join the manager.

After joining the swarm the workers are also referred to as nodes and you’re now ready to create services and deploy them on the swarm cluster.

As you already know to run an instance of my web server. I run the docker run command and specify the name of the image I wish to run.

$ docker run my-web-server:5.4.2

This creates a new container instance of my application and serves on my web server.

Now that we have discussed, how to create a swarm cluster and How do I utilize my cluster to run multiple instances of my web server.

Now one way to do this would be to run the docker run command on each worker node.

But that’s not ideal as I might have to log into each node and run this command and there could be hundreds of nodes.

I will have to setup load balancing myself allowed to monitor the state of each instance myself and if instances where to fail I’ll have to restart them myself.

So it’s going to be an impossible task and that is where Docker swarm orchestration concerning Dockers swarm orchestrator does all of this for us.

Docker Service

Docker services are one or more instances of a single application or service that runs across the nodes in the swarm cluster.

For example, in this case, I could create a Docker service to run multiple instances off my web server application across worker nodes in my swarm cluster.

For this I run the docker service create command on the manager node and specify my image name there which is my web server in this case and use the option replicas to specify the number of instances of my web server I would like to run across the cluster.

$ docker service create --replicas=3 my-web-server

Since I specified three replicas and I get three instances of my web server distributed across the different worker nodes.

Remember the Dockers service command must be run on the manager node and not on the worker node.

The docker service create command is similar to the docker run command. In terms of the options past such as the -e environment variable the -p for publishing ports the network option to attach container to a network etc.

Next container orchestration solution is Kubernetes. You will learn more about Kubernetes in our Kubernetes Tutorials.

Docker Swarm
Scroll to top