How to run an Application using Docker Swarm
Tags: docker, dockerswarm, devops
Published: 2020-05-17 09:53:53

# What is Docker Swarm?

An [Orchestration](https://docs.docker.com/get-started/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](https://kubernetes.io/docs/setup/)  and  [Apache Mesos](http://mesos.apache.org/documentation/latest/).

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

What you'll need:

- Install [Docker](https://www.docker.com/get-started)

- Install [Docker machine](https://docs.docker.com/machine/install-machine/#installing-machine-directly) 

- 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](https://cdn.hashnode.com/res/hashnode/image/upload/v1589702886765/5_jeO46QG.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](https://cdn.hashnode.com/res/hashnode/image/upload/v1589703321878/N-QVZH6Gj.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](https://cdn.hashnode.com/res/hashnode/image/upload/v1589703542146/mjd6XF3_d.png)

## 2. Create a Swarm

![swam architecture.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1589702291638/XhWS85mcZ.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](https://cdn.hashnode.com/res/hashnode/image/upload/v1589704078819/Rx84dqPuM.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](https://cdn.hashnode.com/res/hashnode/image/upload/v1589704282295/zPo7kgvUh.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](https://cdn.hashnode.com/res/hashnode/image/upload/v1589704441910/okJ-gJfLw.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](https://cdn.hashnode.com/res/hashnode/image/upload/v1589704408546/84sKJVM-1.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](https://cdn.hashnode.com/res/hashnode/image/upload/v1589704951222/jvpXQC6Ta.png)

To see the service we've created, run 

``` docker service ls ```

![Screenshot 2020-05-15 at 19.34.27.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1589705163467/5YepIDbTK.png)

To List the tasks under a particular service  run:

``` docker service ps serviceName```

![Screenshot 2020-05-15 at 19.35.56.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1589705278009/v9ZMCt0aS.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](https://cdn.hashnode.com/res/hashnode/image/upload/v1589706180376/iy-eOdNiT.png)

###  [Other commands](https://docs.docker.com/engine/reference/commandline/service/) 

- 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!  💚