Open In App

Continuous Deployment with Docker Swarm: Automating Container Releases

Last Updated : 23 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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
Launch two instances

Step 2: Install Docker

  • Now install Docker on both instances by using following command
sudo yum -y install docker
Command to Install Docker On Redhat/CentOS

Now start and enable docker engine by following commands

sudo systemctl start docker
sudo systemctl enable docker
sudo systemctl status docker
Command to Start,Enable & Check Status Of 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.
Initialize docker swarm

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>
Get join command

Verify Worker Node

  • On the manager node, list the nodes to verify that the worker node has joined:
docker node ls
Verify worker node

Step 5: Create and Deploy a Docker Compose File

Prepare the Docker Compose File

  • On the manager node, create a docker-compose.yml 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"

Docker Compose File


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

.gitlab-ci.yml File

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.
Adding Gitlab CI/CD Environment Variables

Add the following variables:

  • DOCKER_USERNAME: Your Docker Hub username.
  • DOCKER_PASSWORD: Your Docker Hub password.
CI/CD Variables

Step 7: Pushing Code to GitLab

Commit your changes:

git add .
git commit -m "Initial Commit"
Commit your changes

Push to GitLab:

git push origin main
Push code to Gitlab

We can see in gitlab

Gitlab


Step 8: Monitoring the Pipeline

  • Go to your GitLab project and click on CI/CD > Pipelines to view the status of your pipeline.
Monitoring the Pipeline
Deployment of Project

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>
Adjust Scaling Based on Traffic

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.


Next Article
Article Tags :

Similar Reads