Deploying a Web Application on Docker Swarm
Tags: webdev, devops, aws, cloud
Published: 2023-12-08 23:10:12

**Introduction**
In my last article, I automated the creation of a Docker image for an e-commerce website and pushed the image to docker hub. Click the link below for the article.
https://dev.to/gbenga700/automating-infrastructure-deployment-with-docker-and-terraform-deploying-a-containerized-e-commerce-website-5hkp
In this project, I will use the same image to deploy the application using Docker Swam. See below link to the Docker image.
https://hub.docker.com/repository/docker/ojosamuel/e-commerce_terraform/general
Before we proceed, let’s get some basic understanding of Docker Swam as an orchestration tool.

**What is Docker Swarm?**
Docker Swarm is a container orchestration tool that facilitates the management and deployment of containerized applications. It extends the capabilities of Docker containers to a clustered environment, allowing organizations to scale their applications seamlessly. Docker Swarm enables the creation of a swarm, a group of Docker hosts, to deploy and manage containers across multiple nodes.
**Key Features of Docker Swarm:**
**Orchestration:** Docker Swarm provides orchestration capabilities, allowing users to define and manage complex multi-container applications effortlessly. It automates the deployment, scaling, and maintenance of containerized applications, ensuring efficient resource utilization.
**Scalability:** One of the significant advantages of Docker Swarm is its ability to scale applications horizontally. As the demand for resources increases, additional nodes can be added to the swarm, distributing the workload, and improving application performance.
**High Availability:** Docker Swarm ensures high availability by distributing containers across multiple nodes. In the event of a node failure, the swarm automatically reschedules containers to healthy nodes, minimizing downtime and enhancing application reliability.
**Load Balancing:** Load balancing is a crucial aspect of managing containerized applications. Docker Swarm includes an integrated load balancer that distributes incoming traffic among the containers within the swarm, optimizing resource utilization and improving overall application performance.
**Service Discovery:** Service discovery in Docker Swarm is simplified, allowing containers to communicate with each other seamlessly. Each service within the swarm is assigned a DNS name, making it easy for containers to discover and connect to other services.
**Security:** Docker Swarm provides robust security features, including built-in support for Transport Layer Security (TLS) to encrypt communication between nodes. Additionally, access control mechanisms and role-based authentication contribute to creating a secure containerized environment.

**Step 1: Spin-up 3 Ubuntu servers in the AWS management console.** 

• Launch 3 Ubuntu Server using the default VPC and Subnet.
• Instance type- t2.micro (free tier)
• Keypair – Create a new one or use an existing keypair.
• Create a security group and open ports 22, 80 and 2377. 
• Using “user data” to pass a script to install docker on all the 3 servers.
• Rename the servers for easy identification

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sehfx68svzafa7edrvix.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l0zkbto98dhzqn13v0vv.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r35s23mr9yo18wct3rum.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2lx2qqtyi9rb5j5g5njd.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/20c9g5jw10dqi89dq5s4.png)

- Servers renamed. 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xx6iybxd48ixdf50xx65.png)


• Verify that Docker is running on all 3 servers. 
**docker_swarm_manager** – Connect to the server using EC2 instance Connect and run `sudo systemctl status docker` command.



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ob01fp4s38qqxh9nx72a.png)



**docker_swarm_worker1** – Connect to the server using EC2 instance Connect and run `sudo systemctl status docker` command.



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tx2jh2taz1d1h8ojan6z.png)



**docker_swarm_worker2** – Connect to the server using EC2 instance Connect and run `sudo systemctl status docker` command.



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oclih7hvhtfhlcqbhgu1.png)



**Step 2: Initialize docker swarm on _docker_swarm_manager_.** 

- Initialize docker swarm

Run 

```
docker swarm init ---advertise -addr 18.117.82.82
```
 command



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9l5m7vg3iqqcjvkt0ure.png)



- Add a worker to this swarm
Run the following command as displayed after the `docker swarm init` command above

```
docker swarm join --token SWMTKN-1-5qbi2a2vvdaxe35u0xa1wbkbuhkc3ip7vnd5vcpelp1zuuvk40-8uaw7snh5ztn12v589hu82tai 18.117.82.82:2377
```
We will run this command on both worker1 and worker2 to add them to the swarm cluster.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ody6bun8a2w05v9qlqfe.png)



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i3sw4fnnaqn8s64h7ehr.png)





Run `docker node ls` command to view information about nodes in the swarm cluster - 3 nodes in the swam cluster ( 1 manager node and 2 worker nodes)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jk6lwlroktl7gfckznj8.png)

**Step 3: Create service in the  _docker_swarm_manager_.**
The create service command will install the application through the docker image from the docker hub. Run command 

```
 docker service create --name e-commerce_website --replicas 3 --publish 80:80 ojosamuel/e-commerce_terraform:latest
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h20kv2u51g8260i43ky8.png)

Now we have 3 services running. lets confirm with command on the manager node. 

```
docker service ls
```
 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cvgbns9jto9k9yc8sz7b.png)

Lets check if the containers are deployed and running on the nodes with the command 

```
docker container ps 
```

**docker_swarm_manager**


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qrehier5ji2jdspkzak9.png)

**docker_swarm_worker1**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kflxdmep4vcu6o6wxrvr.png)

**docker_swarm_worker2**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4pzcxyrveisqzq5k6nh5.png)

Lets verify that our application is available and accessible from the public IP addresses of each node within the swarm cluster.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/66g2p8i3ekiwf0e2lbw1.png)

Copy and paste the public address in your browser in the format below referencing the port mapped to the container port on your local machine.

```
ip address:port 
18.117.82.82:80
18.189.20.246:80
18.116.35.208:80
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a5ef2hc1dzb23er1ljo5.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2kxgivblh0z15v1nv6pf.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zwwmyf37820y9hf18xh0.png)

Conclusion:
Docker Swarm simplifies the management of containerized applications, offering a robust and scalable solution for organizations embracing container technology. With features like orchestration, scalability, high availability, and security, Docker Swarm provides a comprehensive platform for deploying and managing distributed applications efficiently.

Thanks for Reading!!!