DOCKER SWARM
Tags: docker, aws, devops, cloudskills
Published: 2022-07-08 09:14:26

**WHAT IS DOCKER?**
Docker is a tool which is used to automate the deployment of applications in lightweight containers so that applications can work efficiently in different environments.
**Point to be noted-** Container is a software package that consists of all the dependencies required to run an application.

Advantages of docker-

- Multiple containers run on the same hardware

- Maintains isolated applications

- High productivity

- Quick and easy configuration


**WHAT IS DOCKER SWARM?**
Docker swarm is a service which allows user to create and manage a cluster of Docker nodes and schedule containers.

Each node of a Docker Swarm is a Docker daemon and all Docker daemons interact using the Docker API.

Here, services can be deployed and accessed by nodes of same cluster.


**Now lets see how we can set-up Docker Swarm using AWS EC2 instances.**

First of all create at least 4 EC2 instances. One of them will be used as master node and other three will be used as worker node.
Allow ssh, http and https in the inbound rules of the security group.
 

![EC2 instances](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zhr0dbb43i8iu8jc5x4u.png)

Then we need to install docker in all the 4 instances using the command "yum install docker* -y"

![docker installation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jo20t751il1mdk7ribt0.png)
 
Once installation is completed we need to start docker by the following commands

- systemctl start docker
- systemctl enable docker

Now we need to configure the master node as the master node. the command for that is 

- docker swarm init


![docker swarm init](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l9y5u80b0pci7azte3kr.png)

To convert all the other nodes into worker node we need to copy the highlighted area given in the above picture and past that command in all other nodes which we want to make as worker node.

To check all the available docker nodes we can write

- docker node ls

![docker nodes](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tn48ibu56sjox7vqy597.png)

Then we need to create a docker file where we will write the script to launch an image and provide the web page which we want to launch.
To create the docker file we need to write "vi Dockerfile"


![docker files](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4etu03xzjtuy9wcjy22d.png)

Now we need to built an image.


![docker image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ayqa365vn85bine9m9pv.png) 

We need to perform the above two steps in all the worker nodes too.

Then we need to create service.
-docker service create --name app-server -p 80:80 appimage:1

 
![services](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x5akw109j2n81srpduze.png)

It will create a container in any one node but if we hit the public IP address of any of the instance we can see the web page is running regardless in which instance the container is.


![public ip](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bpjve1x51tb7pyed5cdj.png)


![web page](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gntxo7gus341e5aloirt.png)

It is very easy to increase or decrease the numbers of services in docker, we can simply do it by the command given below.

![increase service](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1nuwe1xst3fo4wzboqrj.png)

We can also view our containers using a docker visualizer, which is a graphical representation of all the containers and instances.
To set-up visualizer we need to run the given command.

- docker service create --name=viz --publish=8080:8080/tcp --constraint=node.role==manager --mount=type=bind,src=/var/run/docker.sock,dst=/var/run/docker.sock dockersamples/visualizer


![docker visualizer](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zg6glky8qdydz21w4z1c.png)

Now if you hit the public ip of the master node mapped with port 8080 you can see the Docker visualizer.


![docker visualizer](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j2mhm6c0cd0wmfs0pi0t.png)

This was the whole process of how to set-up docker swarm.