GitLab CI/CD is a software development solution that enables enterprises to implement “continuous” approaches such as continuous integration (CI), continuous delivery (CD), and continuous deployment (CD). You may detect code flaws and bugs early in the software development life cycle (SDLC) by using GitLab CI/CD. It assists you in ensuring that any code you deploy to production adheres to the necessary compliance regulations and coding standards for your apps. Read about how to unregister a GitLab Runner. Sometimes it might be necessary to archive, unarchive or delete a GitLab Project. In this post, I will be showing you how to Build Docker Images with GitLab CI.
One of the most used cases for CI pipeline is building Docker images and using them to deploy your application. It is a wonderful option because it includes a built-in registry to store your generated images and an integrated pull proxy service, resulting in building pipelines faster. You may need to install Docker Desktop and register GitLab-Runner with Docker-windows executor to get along in this post if you’re running a Windows Machine.
You can use GitLab to create Docker images and push them to the integrated container registry by adding a job to your pipeline.
How to Build Docker Image with GitLab CI
A file called
.gitlab-ci.yml located at the root of your project is used to configure GitLab CI. This is utilized automatically. With GitLab, it is possible to add a job to your pipeline that will create Docker images and submit them to the built-in container registry.
Of course, how exactly everything is set up will be highly dependent on you and your requirements. Please see how to fix “The executor requires OSType=windows, but Docker Engine supports only OSType=linux“, How to uninstall GitLab from your Windows device, and Install and Register GitLab Runner on Windows.
Prerequisites to Building Docker Images
To do this successfully, you will need a gitlab-runner with docker-in-docker (dind) configured and a working Dockerfile. It is easy for you to setup GitLab Runner on Windows WSL to get your gitleb-runner to use Docker as an executor.
The first step to carry out is to build the job.
Step 1: Clone Project Repository
Locate the Git repository for the project you want to build images for. Create a
.gitlab-ci.yml file at the root of the repository. This file defines the GitLab CI pipeline that will run when you push changes to your project.
We have a project called
testci project that we need to clone to our local machine from GitLab.
Step 2: Set Registry Authentication with Docker Login
The simplest way to configure registry authentication is to use the
docker login command. In the root ofyour GgitLab project, locate Deploy>Container Registry
Please see how to fix “Failed to remove network for build: Error during connect in the default daemon configuration on Windows, the Docker client must be run with elevated privileges“, and “How to build your first CI/CD Pipeline in Azure DevOps using ASP.Net Core Application“.
Step 3: Inject the GitLab Predefined Variables into your .gitlab-ci.yml file
You can map the GitLab predefined variables to the Docker Login in your Yaml file. To locate the GitLab CI/CD predefined variables, click here. For this article, we are only going to use the
CI_REGISTRY predefined variables in our
.gitlab-ci.yml file to pass in the
GitLab predefined variables are extremely useful for controlling jobs and pipelines, and they help you avoid hard-coding values in your
.gitlab-ci.yml configuration file.
Step 4: Get your .gitlab-ci.yml and Dockerfile ready
Here, we want to build a simple Docker image using Python, and we defined our
.gitlab-ci.yml file and
Dockerfile as follows:
Building a Docker Image: image: python:3.11 script: - python --version - pip --version - pip install pytest - pytest --version - echo "My first GitLab CI" build image: image: docker services: - docker:dind script: - echo $CI_REGISTRY_PASSWORD | docker login -u $CI_REGISTRY_USER $CI_REGISTRY --password-stdin - docker build -t $CI_REGISTRY_IMAGE . - docker push $CI_REGISTRY_IMAGE
In the .gitlab-ci.yml above, we simply tell Docker login to get the password from the standard input (stndin).
FROM python:3.11 RUN pip install pytest
Please see How to install WSL2 on Windows Server, How to fix Windows Microsoft Store not opening, and How to Disable and Enable Registry Access on Windows 11.
Step 5: Commit and push changes to Remote Project Repository
Add the files by running the
git add . command followed by the
git commit command, specify the branch and then push it to the remote repository as shown in the screenshot below:
.gitlab-ci.yml file and
Dockerfile to the remote repository will trigger a build automatically from the Docker Image in the GitLab Container Registry once the push is complete. From the screenshot below, it shows that the job was succeeded.
There will be an email alert showing that the pipeline has passed successfully.
If you check the GitLab Container Registry, you should now see the image right inside the container registry.
Here are some exciting guides: How to Stop OneDrive from Starting Up Automatically on Windows 11, how to Build a Scalable VPC for Your AWS Environment, and How To Use Logrotate For Managing Log Files In Ubuntu Linux.
Exending the Build Job
Now, let’s extend the job to run on schedule by adding some additional steps to the
.gitlab-ci.yml file as shown below:
Creating a Schedule on GitLab CI/CD
Head to your GitLab project and create a schedule. To create a schedule, from GitLab project overview page > Build > Pipeline schedules > New schedules
Lastly, commit and push the changes to build a new pipeline job automatically on your GitLab project.
I hope you found this article useful on how to build Docker Images with GitLab CI. Please let me know in the comment section if you have any questions.