Skip to content

TechDirectArchive

Hands-on IT, Cloud, Security, Veeam & DevOps

  • Home
  • About
  • Advertise With US
  • Reviews
  • Contact
  • Toggle search form

Build Docker Images with GitLab CI

Posted on 29/06/202314/08/2026 Imoh Etuk By Imoh Etuk No Comments on Build Docker Images with GitLab CI
  1. Home
  2. Automation
  3. 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.

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

  • ansible 1
    Kinit Error: Fix Malformed representation of principal when parsing name Configuration Management Tool
  • PowerShell Module Must be set to Remote Signed e1779570167985
    Veeam Backup and Replication: PowerShell must be Remote Signed Backup
  • Chocolatey Packages
    Create Chocolatey Package: Upgrade Software with Chocolatey Scripts
  • aptira ansible 1
    The module ping was not found in configured module paths, core modules are missing Configuration Management Tool
  • Azure DevOps Server
    How to Install Azure DevOps Server 2022 Automation
  • image 117
    Deploy MVC Application to AWS EC2 Using RDP and Web Deploy Configuration Management Tool

More Related Articles

maxresdefault 2 How to uninstall Applications with PowerShell Script Scripts
windows update 03 How to automate Windows Update with PowerShell Scripts
powershell commands lede 1024x276 1 PowerShell Remoting: Guide to Windows Management Instrumentation Scripts
Migrate from Universal Analytics to GA4 property Configure and Migrate from Universal Analytics to GA4 property Automation
wacxx Test Network Connection to Windows Admin Center Gateway Scripts
Opswork blog 1440x800 1 AWS Opsworks For Chef Automate Configuration Management Tool

Leave a Reply Cancel reply

You must be logged in to post a comment.

Microsoft MVP

vexpert-badge-stars-5

Virtual Background

VEEAMLEGEND

Categories

Top 30 Sysadmin blog

veeaam100

Veeam Vanguard

  • diagnose
    How to Diagnose a Windows Hardware Issue Windows
  • Feature image
    When Should I Use TPM or TPM + PIN Security | Vulnerability Scans and Assessment
  • screenshot 2020 03 01 at 21.19.07
    Various ways to hide Windows Taskbar Windows
  • images copy
    How to disable automatic screen lock in Ubuntu Linux Linux
  • ACMP Defender Management
    How to Manage Microsoft Defender Antivirus with Argon ACMP Network | Monitoring
  • image 41
    How to Quickly Fix Windows Search Bar Not Working Windows
  • Remote Desktop 2 1
    How to install RDS via Quick Start Deployment: Install, Publish, Update, and Uninstall Remote Desktop Web Client Web Server
  • Prevent Standard Users from Changing BitLocker Password
    How to Prevent Standard Users from Changing BitLocker Password Windows

Subscribe to Blog via Email

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

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

Tags

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

Copyright © 2026 TechDirectArchive

Loading Comments...

You must be logged in to post a comment.