Skip to content

TechDirectArchive

Hands-on IT, Cloud, Security & DevOps Insights

  • Home
  • About
  • Advertise With US
  • Reviews
  • Contact
  • Toggle search form
Home » Scripts » Build Docker Images with GitLab CI

Build Docker Images with GitLab CI

Posted on 29/06/202302/07/2023 Imoh Etuk By Imoh Etuk No Comments on Build Docker Images with GitLab CI
CI-With-GitLab-1
CI with GitLab

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.

clone-clone-repo
Cloning Remote Repository

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

container_registry_credential
GitLab Conatiner Registry Authentication

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_IMAGE, CI_REGISTRY_PASSWORD, CI_REGISTRY_USER and CI_REGISTRY predefined variables in our .gitlab-ci.yml file to pass in the docker login, build and push variables.

locating-predefined-variables
GitLab Predefined Variables

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:

.gitlab-ci.yml file

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).

Dockerfile

FROM python:3.11
RUN pip install pytest
gitlab-ci-yml-file
dockerfile

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:

commit-and-push-changes
Commiting and Pushing changes to the remote repo

Pushing the .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.

Building-CI-Pipeline
build-piepline-succeeded

There will be an email alert showing that the pipeline has passed successfully.

Email-alert-pipeline-passed
Build Email Alert

If you check the GitLab Container Registry, you should now see the image right inside the container registry.

latest-image-in-gitlab-container
Docker Image in 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:

adding-extra-steps-in-yml-file

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

creating-a-piepline-schedules
Creating Pipeline Schedules

Lastly, commit and push the changes to build a new pipeline job automatically on your GitLab project.

New-pipeline
New pipeline job successfully built

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.

Rate this post

Thank you for reading this post. Kindly share it with others.

  • Share on X (Opens in new window) X
  • Share on Reddit (Opens in new window) Reddit
  • Share on LinkedIn (Opens in new window) LinkedIn
  • Share on Facebook (Opens in new window) Facebook
  • Share on Pinterest (Opens in new window) Pinterest
  • Share on Tumblr (Opens in new window) Tumblr
  • Share on Telegram (Opens in new window) Telegram
  • Share on WhatsApp (Opens in new window) WhatsApp
  • Share on Mastodon (Opens in new window) Mastodon
  • Share on Bluesky (Opens in new window) Bluesky
  • Share on Threads (Opens in new window) Threads
  • Share on Nextdoor (Opens in new window) Nextdoor
Automation, Configuration Management Tool, Scripts Tags:Git Commands

Post navigation

Previous Post: How to install WSL2 on Windows Server
Next Post: How to configure additional LSA Protection

Related Posts

  • banner2
    How to Integrate Postman With GitHub Automation
  • image 28
    How to monitor your website uptime and status using Uptime Configuration Management Tool
  • 7164 1024x575 1
    How to install MDT PowerShell module on Windows Scripts
  • Opswork blog 1440x800 1
    AWS Opsworks For Chef Automate Configuration Management Tool
  • ADO Self hosted agent
    How to Create Self-Hosted Agent for Azure DevOps Pipelines Automation
  • S3 Bucket
    Access AWS Management Console and Create Resources with AWS CLI on Windows AWS/Azure/OpenShift

More Related Articles

banner2 How to Integrate Postman With GitHub Automation
image 28 How to monitor your website uptime and status using Uptime Configuration Management Tool
7164 1024x575 1 How to install MDT PowerShell module on Windows Scripts
Opswork blog 1440x800 1 AWS Opsworks For Chef Automate Configuration Management Tool
ADO Self hosted agent How to Create Self-Hosted Agent for Azure DevOps Pipelines Automation
S3 Bucket Access AWS Management Console and Create Resources with AWS CLI on Windows AWS/Azure/OpenShift

Leave a Reply Cancel reply

You must be logged in to post a comment.

Microsoft MVP

VEEAMLEGEND

vexpert-badge-stars-5

Virtual Background

GoogleNews

Categories

veeaam100

Veeam Vanguard

  • BitLocker Recovery Password Rotation in Active Directory
    Perform BitLocker Recovery Password Rotation in Active Directory Windows Server
  • screenshot 2020 03 13 at 20.24.17
    How to view installed packages in Cygwin on Windows Windows
  • linux tux minimalism 4k 42 2560x1700 1 1
    Warning useradd: the home directory already exists. Not copying any file from skel director into it Linux
  • teams feature
    How to install and configure Microsoft Teams on Ubuntu Linux
  • Windows 10 new Start menu
    Make Cortana search with a different web browser instead of Edge Windows
  • Syncing Files  and photos with Synology Drive
    Sync file and photos from iOS and Mac with Synology Drive Backup
  • tuning tomcat
    How to upgrade Apache Tomcat from One Version to another Web Server
  • Perform an in place upgrade of Windows Server
    In-place upgrade of Windows Server 2022 to 2025 Backup

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 1,821 other subscribers
  • RSS - Posts
  • RSS - Comments
  • About
  • Authors
  • Write for us
  • Advertise with us
  • General Terms and Conditions
  • Privacy policy
  • Feedly
  • Telegram
  • Youtube
  • Facebook
  • Instagram
  • LinkedIn
  • Tumblr
  • Pinterest
  • Twitter
  • mastodon

Tags

Active Directory Azure Bitlocker Microsoft Windows PowerShell WDS Windows 10 Windows 11 Windows Deployment Services Windows Server 2016

Copyright © 2025 TechDirectArchive

 

Loading Comments...
 

You must be logged in to post a comment.