Devops Project by Using Docker Swarm, Git, GitHub, and Jenkins.
Docker Swarm, Git, GitHub, and Jenkins Project
Building and Deploying a Project Using Docker Swarm, Git, GitHub, and Jenkins
==============================================================
Mastering Docker Swarm: Deploying a Scalable Project
In today’s fast-paced DevOps-driven world, container orchestration plays a pivotal role in ensuring applications are deployed and managed effectively. Docker Swarm, Docker’s native clustering and orchestration tool, is one of the most robust and user-friendly solutions for managing containerized applications. In this blog, we’ll take you through a Docker Swarm project that demonstrates how to deploy a scalable application using Docker Swarm clusters.
1. What is Docker Swarm?
Docker Swarm is an orchestration tool that allows you to manage a cluster of Docker hosts as a single virtual system. It simplifies container deployment, scaling, and management while ensuring high availability and load balancing for your applications.
Key features of Docker Swarm include:
- High Availability: Services are distributed across nodes to ensure fault tolerance.
- Scaling: Easily scale services up or down with a single command.
- Load Balancing: Automatically balances requests across containers in the cluster.
- Security: Uses TLS encryption to secure communication between nodes.
- Ease of Use: Seamless integration with Docker CLI.
2. Project Overview
Project Workflow Overview
This project workflow includes three critical stages: code management (Git/GitHub), automation (Jenkins), and deployment (Docker Swarm).
In this project, we will:
- Initialize a Docker Swarm cluster.
- Deploy a Docker service with five replicas on the cluster.
- Use a WAR-based web application packaged into a Docker image.
- Push the image to DockerHub for accessibility.
- Deploy the application to the Swarm cluster using an Ansible playbook.
3. Prerequisites
To follow along, ensure you have the following:
- Docker Installed on all nodes in the Swarm cluster.
- Ansible Installed for automated deployments.
- A DockerHub account to host your image.
- At least one manager node and one worker node configured for the cluster.
4. Setting Up Docker Swarm Cluster
Step 1: Managing Code with Git and GitHub
- Git: A distributed version control system that tracks changes in the source code.
- GitHub: A cloud-based repository platform for hosting, managing, and collaborating on Git projects.
Workflow:
- Developers write and commit code changes using Git.
- The code is pushed to a GitHub repository.
- Branching and pull requests enable collaborative workflows, allowing for code reviews and merges.
For this project, the source code and Docker configurations (e.g., Dockerfile
) were stored in a GitHub repository for easy sharing and version control.
==============================================================
Step 2: Automating with Jenkins
Jenkins, a leading automation server, facilitates continuous integration (CI) and continuous delivery (CD).
Workflow in Jenkins:
- Triggering Builds: A Jenkins pipeline monitors the GitHub repository for changes. Whenever a commit is pushed, it triggers a build process.
- Building Docker Images: Jenkins fetches the source code, builds a Docker image using the
Dockerfile
, and tags the image for Docker Hub. - Pushing to Docker Hub: The built Docker image is pushed to Docker Hub for use in the deployment phase.
- Deployment Automation: A post-build action in Jenkins executes an Ansible playbook to deploy the application on the Docker Swarm cluster.

=========================================================================
a. Initialize the Swarm Cluster
Run the following command on the manager node to initialize the Swarm:
sudo docker swarm init --advertise-addr <manager_ip>
This will return a token to add worker nodes to the Swarm.
b. Join Worker Nodes
On each worker node, run the token command provided by the manager:
sudo docker swarm join --token <token> <manager_ip>:2377
c. Verify Cluster Status
Check the status of your Swarm cluster from the manager:
docker node ls
You should see all nodes in the cluster.
5. Building and Pushing the Docker Image
a. Prepare the Dockerfile
Create a Dockerfile
to package your web application:
FROM tomcat:9.0
COPY webapp.war /usr/local/tomcat/webapps/
b. Build the Docker Image
On your local machine, build the image:
docker build -t jan2025pro:latest .
c. Tag and Push to DockerHub
Tag and push the image to your DockerHub repository:
docker tag jan2025pro:latest vikramsinhshinde/jan2025pro:latest
docker push vikramsinhshinde/jan2025pro:latest
6. Deploying the Application with Docker Swarm
Ansible’s Role in Deployment
Ansible automates the deployment process by managing the Swarm cluster and executing tasks such as removing old services, creating new ones, and ensuring proper configurations.
a. Create an Ansible Playbook
Here’s an Ansible playbook to deploy the project:
---
- name: Deploy Project Docker Image on Swarm Cluster
hosts: swarmcluster
become: yes
tasks:
- name: Remove existing running service
shell: docker service rm registration
ignore_errors: yes
- name: Deploy the new service
shell: docker service create --replicas=5 -d -p 8080:8080 --name registration vikramsinhshinde/jan2025pro:latest
Save this playbook as deploy.yml
.
b. Run the Playbook
Execute the playbook to deploy your application:
ansible-playbook -i inventory deploy.yml
7. Verifying the Deployment
After deployment, verify that the service is running:
- Check the status of the Swarm services:
docker service ls
- Verify that all replicas are running:
docker service ps registration
- Access your application in a browser at
http://<manager_ip>:8080
.
8. Scaling the Application
Scaling your application is as simple as running a single command. For example, to scale the service to 10 replicas:
docker service scale registration=10
Docker Swarm will automatically distribute the replicas across the available nodes.
9. Handling Failures
Docker Swarm’s self-healing capabilities ensure high availability. If a node fails, Swarm re-assigns tasks to other nodes. You can test this by stopping a node and observing the re-distribution of tasks:
sudo systemctl stop docker
End-to-End Workflow Recap
Git and GitHub:
- Developers push code to a GitHub repository.
- Jenkins pulls the latest code for the build process.
Jenkins CI/CD Pipeline:
- Automates building and pushing the Docker image to Docker Hub.
- Executes an Ansible playbook to deploy the application.
Docker Swarm:
- Ensures high availability and scalability by distributing services across the cluster.
Advantages of This Workflow
- Automation: Reduces manual intervention, enhancing productivity and efficiency.
- Scalability: Docker Swarm’s replication feature ensures the application scales seamlessly.
- Collaboration: Git and GitHub enable collaborative development and version control.
- Flexibility: Jenkins pipelines allow customization for diverse workflows.
10. Conclusion
With Docker Swarm, deploying and managing scalable applications becomes streamlined and efficient. By combining Docker’s powerful containerization with Swarm’s orchestration capabilities, this project highlights how to achieve fault-tolerant, load-balanced deployments.
This project demonstrates the power of integrating Git, GitHub, Jenkins, Docker Swarm, and Ansible to achieve a robust, automated CI/CD pipeline. By leveraging these tools, teams can build, test, and deploy applications efficiently, ensuring scalability and reliability in production environments.
Whether you’re new to container orchestration or an experienced DevOps engineer, Docker Swarm remains a reliable choice for building scalable infrastructures. Start exploring its potential in your projects today!
--By Vikramsinh Shinde
Would you like to delve deeper into advanced Swarm topics, such as rolling updates or securing your cluster? Let us know in the comments!
Comments
Post a Comment