Implementing GitLab CI/CD with Docker Swarm, Portainer, and Private Registry in a Local Environment — Part 1/3
Introduction
Continuous Integration (CI) and Continuous Delivery (CD) are essential practices in modern software development. They enable developers to automate the processes of building, testing, and deploying applications, resulting in faster delivery cycles, improved quality, and reduced risk. In this three-part series, we will delve into the implementation of GitLab CI/CD in your own local environment, allowing you to experience everything firsthand.
Architecture
The architecture will include GitLab, Docker Swarm, Portainer as our container manager, and a Private Registry to host our Docker images. All components will be hosted locally, making this architecture highly customizable and providing an excellent testing environment.
1- Virtual Machines Configuration and Setting up Docker and Docker Swarm (Part 1)
2- Portainer, Private Registry, Local GitLab, and GitLab runners as containers (Part 2)
3- Testing Our Architecture Through the Implementation of the CI/CD Workflow (Part 3)
Virtual Machines Configuration and Setting up Docker and Docker Swarm
For our implementation, we will need three VMs: one acting as a manager and two as workers, forming a swarm cluster.
I will be using VMware for virtualization, but feel free to choose any virtualization platform that aligns with your preferences. We will use Debian 12, but you can go for any Linux-based distribution that suits you.
Starting with the Manager node, it needs a slightly stronger machine as running containerized GitLab Community Edition (CE) Image will require at least 4 GB of RAM and 2 processors.
For the workers, we will allocate minimal resources, going for 2 GB of RAM and 1 processor with a single core.
Since we are working locally and there is no need for a firewall or ACL, we won’t have to deal with port configurations. Just ensure that the VMs are on the same network.
Installing Docker and Configure our swarm
We will start off by installing Docker, I suggest checking out the Official Docker Wiki for more information “Install Docker Engine on Debian”.
In each VM (Manager, worker-1, and worker-2), execute the following code:
#update and upgrade installed packages on the system
sudo apt-get update -y && apt-get upgrade -y
# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl gnupg
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/debian/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg# Add the repository to Apt sources:
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/debian \
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update#Install Docker Engine
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin#Test Docker
sudo docker run hello-world
Now that Docker is installed, we can move on to configuring our Docker Swarm cluster. In the Manager Node:
#In the Manage Node use the flowing commmand
sudo docker swarm init
#A New command will be generated allowing us to add worker nodes to the Swarm
#Copy and Execute it in the Workers VM
To verify if the workers are added and ready, use the following command on the Manager Node:
docker node ls
the only thing left is creating a network for our private registry and Gitlab/GitLab Runners (you can use the default one).
#In the Manage Node :
docker network create -d overlay gitlab-network
Conclusions
In this part, we’ve successfully set up the foundation of our local environment. With our virtual machines configured and Docker Swarm initialized, we are ready to proceed to Part 2, where we’ll focus on deploying Portainer, configuring a Private Registry, and incorporating GitLab and its runners into our containerized ecosystem.