DEV Community

Cover image for How to run an Application using Docker Swarm
Bolton
Bolton

Posted on

How to run an Application using Docker Swarm

What is Docker Swarm?

An Orchestration tool. An Orchestrator is basically a tool that helps you manage, scale, and maintain your containerized applications.
In other words, this tool helps in scaling your application, automating its maintenance, replacing failed containers automatically, and rolling out updates. Other examples of this tool are Kubernetes and Apache Mesos.

In this article, I will take you through managing and controlling multiple docker containers as a single service.

What you'll need:

  • Install Docker

  • Install Docker machine

  • Install Virtual box. we'll use this to create docker machines.

> sudo apt-get install Virtualbox

or on Mac

> brew cask install virtualbox

1. Create 3 docker-machines

Create a docker-machine called manager which will act as the manager.

> docker-machine create --driver virtualbox manager

Screenshot 2020-05-15 at 18.58.43.png

The next step would be to create 2 more machines which will act as worker machines by running

> docker-machine create --driver virtualbox worker1

> docker-machine create --driver virtualbox worker2

You can check all the machines by running:

> docker-machine ls

Screenshot 2020-05-15 at 19.05.55.png

You can now connect to any of the machines by running:

> docker-machine ssh manager

Screenshot 2020-05-15 at 19.08.31.png

2. Create a Swarm

swam architecture.png

To create a swarm, ssh into the machine which will act as the manager (machine named manager in our case) and run:

> docker swarm init --advertise-addr MANAGER_IP

To get the manager IP, run:

> docker-machine ip manager

Screenshot 2020-05-15 at 19.14.06.png

The next step would be to add the two worker machines to the Swarm we've just created.
While on the manager run the command below to get the actual command to use.

docker swarm join-token manager

Screenshot 2020-05-15 at 19.20.19.png

Copy the generated code and ssh into each of the worker machines to join them.

Screenshot 2020-05-15 at 19.22.14.png

While on the manager machine, run this command to see the created nodes

docker node ls

Screenshot 2020-05-15 at 19.24.11.png

As shown above, you can see we have 3 nodes and the machine named manager as the Leader.

3. Running the application

In the manager machine, we are going to run three replicas of the application by running:

docker service create --replicas 3 -p 80:80 --name serviceName dockerImage

serviceName is the name of our application and you can use any Docker image you have. In this instance, I'm going to use Nginx.

Screenshot 2020-05-15 at 19.33.14.png

To see the service we've created, run

docker service ls

Screenshot 2020-05-15 at 19.34.27.png

To List the tasks under a particular service run:

docker service ps serviceName

Screenshot 2020-05-15 at 19.35.56.png

3. Test the application

Copy the Ip of one of the docker machines and you'll see the Nginx service running.

Screenshot 2020-05-15 at 19.38.33.png

Other commands

  • Display detailed information on one or more services

    docker service inspect serviceName

  • Fetch the logs of service or task

    docker service logs serviceName

  • Remove a service

    docker service rm serviceName

  • Scale one or multiple replicated services

    docker service scale serviceName=2

And that's all folks. Thank you for reading through. If this article was helpful, feel free to share it.

Happy Coding! ๐Ÿ’š

Oldest comments (2)

Collapse
 
scalzadonna profile image
Santi Scalzadonna

Thanks Bolton for your introduction. It's really a great start. I would like to see it working with a webapp or something more informative. Keep me posted about it!

Collapse
 
boltc0rt3z profile image
Bolton

Thanks Santi.