Continuous Deployment with Docker Swarm: Automating Container Releases
Last Updated :
23 Sep, 2024
In software development, specifically for software update and feature addition, it is a very agile world. Continuous Deployment (CD) is crucial and is supported by such practices to automate the frequent delivery of new features and/or updates in coding changes to the production environment with minimal manual involvement.
Docker Swarm, a native clustering and orchestration tool of Docker, has provided a method to effectively and scalably manage applications in containers. By using Docker Swarm, development teams are now enabled to automatically deploy containers, thus enabling them to understand how the updates have to be put forward so that it reaches across a cluster of Docker nodes.
In this post, we will describe how to use Docker Swarm so that your current Continuous Integration situation can lead to Continuous Deployment, and therefore optimize your development workflow. This will cover basic concepts of Docker Swarm, a step-by-step configuration guide for your application deployment, and some best automation practices for container releasing. If you are new to Docker Swarm or trying to level up your deployments, this guide has you covered to make the best of your deployment process.
Primary Terminologies
- Continuous Deployment (CD): Continuous Deployment (CD) is a software engineering practice in which every change that has passed through all stages of the pipeline is automatically deployed to production. This ensures users get updates in a fast and reliable way.
- Docker Swarm: Docker's native clustering and orchestration convert a fleet of Docker hosts into one big, logical swarm for deploying, managing, and scaling containerized applications among multiple hosts.
- Service: An abstraction in Docker Swarm that describes running features of a containerized application across the entire cluster. It specifies a Docker image, the number of replicas to run, and other options like network settings and update policies.
- Stack: A stack is a collection of services that are deployed together as one unit in Docker Swarm. In its definition, it is a Compose file inside Docker that describes services, networks, and volumes needed for an application.
- Task: The atomic unit of scheduling in Docker Swarm, a task represents a single container running as part of a service. Each replica of a service corresponds to a task.
- Node: A node is a single host on which there is an installation of Docker and that is part of the Docker Swarm cluster. The managers are those nodes with the capacity to direct the working of the other nodes in the cluster, while workers are those that run the jobs directed by the managers.
- Manager Node: A node acting as a manager within a Docker Swarm; this is the member orchestrating and managing the swarm, which assigns tasks to worker nodes, maintains the state of a service, and attempts to maintain the desired state of a cluster.
Step-by-Step Process Of CD with Docker Swarm
Step 1: Launch Two Instances
- Here we are launching two instances one for master node and another for working node
Step 2: Install Docker
- Now install Docker on both instances by using following command
sudo yum -y install docker
Now start and enable docker engine by following commands
sudo systemctl start docker
sudo systemctl enable docker
sudo systemctl status docker
Step 3: Initialize Docker Swarm on the Manager Node
Initialize Docker Swarm
On the manager node, initialize the Docker Swarm:
docker swarm init
- The output will include a command to join other nodes to the Swarm. Note the join token and manager IP address.
Step 4: Join the Worker Node to the Swarm
Get Join Command
- Use the join command provided by the
docker swarm init
output from the manager node. It looks like this:
docker swarm join --token <token> <manager-ip>:<port>
Verify Worker Node
- On the manager node, list the nodes to verify that the worker node has joined:
docker node ls
Step 5: Create and Deploy a Docker Compose File
Prepare the Docker Compose File
- On the manager node, create a
docker-compose.ym
l
file with the services you want to deploy. For example:
version: '3.8'
services:
web:
image: sadasiva07/my_image:latest
deploy:
replicas: 3
update_config:
parallelism: 2
delay: 10s
restart_policy:
condition: on-failure
ports:
- "8080:8080"
Step 6: Creating a .gitlab-ci.yml
File
stages:
- build
- test
- deploy
variables:
DOCKER_IMAGE: my_image:latest
# Step 1: Build the Docker image
build:
stage: build
script:
- docker build -t $DOCKER_IMAGE .
- docker login -u "$DOCKER_USERNAME" -p "$DOCKER_PASSWORD"
- docker push $DOCKER_IMAGE
# Step 2: Run tests
test:
stage: test
script:
- docker run --rm $DOCKER_IMAGE pytest # Replace pytest with your actual test command
# Step 3: Deploy to Docker Swarm
deploy:
stage: deploy
script:
- docker stack deploy -c docker-compose.yml my_image
only:
- main # Deploy only when changes are pushed to the main branch
Adding GitLab CI/CD Environment Variables
You need to add your Docker registry credentials to your GitLab project's CI/CD variables.
- Navigate to your project in GitLab.
- Go to Settings > CI/CD > Variables.
Add the following variables:
DOCKER_USERNAME
: Your Docker Hub username.DOCKER_PASSWORD
: Your Docker Hub password.
Step 7: Pushing Code to GitLab
Commit your changes:
git add .
git commit -m "Initial Commit"
Push to GitLab:
git push origin main
We can see in gitlab
Step 8: Monitoring the Pipeline
- Go to your GitLab project and click on CI/CD > Pipelines to view the status of your pipeline.
Adjust Scaling Based on Traffic:
- Adjust the number of replicas based on your application's traffic load using:
docker service scale my_image_web=<desired_replicas>
Conclusion
Continuous Deployment with Docker Swarm is a powerful way of automatically and seamlessly ensuring the availability of containerized applications. This capability allows the development team to carry out deployments over various nodes in an effective manner that focuses on high availability, fault tolerance, and consistency of performance.
Integrating Docker Swarm into a Continuous Deployment pipeline makes the process of manual deployment less risky and expedites the release cycle, leading to fast feedback and iteration. As organizations move more rapidly toward cloud-native practices, mastery of Docker Swarm for automated container releases can be used to meet the demands of modern software development with agility and certainty.
Similar Reads
Blue-Green Deployments with Docker Swarm
Maintaining operational continuity and user satisfaction with minimal downtime is critical in today's fast-paced digital ecosystem. This article delves into blue-green deployment, an effective strategy designed for application updates with zero downtime. Tailored for DevOps professionals and system
15+ min read
How to Continue a Docker Container Which has Exited
Docker containers have reformed how programming is developed, deployed, and managed, offering unmatched productivity and consistency across various conditions. In any case, overseeing containers, particularly when they've exited, requires a nuanced way to deal with guarantee smooth tasks. At the poi
6 min read
Docker at Scale: Handling Large Container Deployments
Docker technology allows you to combine and store your code and its dependencies in a small package called an image. This image can then be used to launch a container instance of your application. Table of Content What is a Docker Container?Why Use Docker Containers?Step-By-Step Guide to Docker at S
5 min read
Copying Files to and from Docker Containers
While working on a Docker project, you might require copying files to and from Docker Containers and your Local Machine. Once you have built the Docker Image with a particular Docker build context, building it again and again just to add small files or folders inside the Container might be expensive
9 min read
Automating Docker Deployments With Ansible
Docker is a tool used to dockerize the application along with its dependencies, on the other hand, Ansible is a tool used to manage configuration and deploy applications on other machines. Here in this guide, I will first discuss what is Docker. Then I will discuss What is Ansible. After this, I wil
5 min read
Serverless Computing With AWS Lambda And Docker: Running Custom Containers
AWS Lambda is a serverless computing service that runs the code without any management of servers by the user. On the other hand, Docker is a tool that helps to encapsulate applications with its dependency into docker containers. AWS Lambda and Docker are some of the most important services used by
5 min read
Deployment Automation with Cloud Build
Software delivery is revolutionized by deployment automation, which helps speed up release cycles, lower errors, and streamline deployment procedures. It gives businesses the ability to deliver continuously, which guarantees a quicker time to market and more agility. Automation makes it possible to
5 min read
How to Use Docker For Cross-Platform Containerization with Docker Buildx?
Docker has completely modified the manner in which software program is evolved and deployed, with the aid of introducing containerization generation. In order to package programs with their dependencies and make certain consistency of conduct across many contexts, packing containers offer a compact
7 min read
How To Deploy A Container To Azure Container Instances ?
In layman's terms, how will it feel when a developer develops an app and a member of the testing team tries to test the application on his/her OS? They will have to install the packages, and libraries required to run the app. Well, in this case, the container makes it easy and faster to rapidly depl
8 min read
Docker Compose For Java Applications: Simplifying Deployment
Docker is an open-source platform for developers and sysadmins to build, ship, and run distributed applications. If we want to define and run more than one container then we should go for docker compose. We have to configure multiple container environments in a single file so that it simplifies the
8 min read