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

  • hero activedirectory
    Create and find Organisation Unit paths in AD Scripts
  • Azure App Service
    How to Restore Deleted Azure App Service Using PowerShell Automation
  • powershell
    How to perform PowerShell syntax check using PSScriptAnalyzer Scripts
  • images 5
    IP Address UNREACHABLE: HTTP connection pool Max exceeded Configuration Management Tool
  • powershell01
    How to add servers to the Trusted Hosts list Scripts
  • ansible logo600 591x296 1
    Ansible_user=UNREACHABLE {Failed to connect to the host via SSH: SSH: Could not resolve hostname (Name or service not known, unreachable true) Configuration Management Tool

More Related Articles

hero activedirectory Create and find Organisation Unit paths in AD Scripts
Azure App Service How to Restore Deleted Azure App Service Using PowerShell Automation
powershell How to perform PowerShell syntax check using PSScriptAnalyzer Scripts
images 5 IP Address UNREACHABLE: HTTP connection pool Max exceeded Configuration Management Tool
powershell01 How to add servers to the Trusted Hosts list Scripts
ansible logo600 591x296 1 Ansible_user=UNREACHABLE {Failed to connect to the host via SSH: SSH: Could not resolve hostname (Name or service not known, unreachable true) 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

veeaam100

Veeam Vanguard

  • Featured image Network Access Permission...
    Fix You Might Not Have Permission to Use This Network Resource Error Network | Monitoring
  • PowerShell 1
    How to upgrade PowerShell to the latest version on macOS via Homebrew Mac
  • EFK Stack
    EFK Stack Installation on Docker Virtualization
  • VHDX resizing and veeam back
    Hyper V Disk allocation: Why Veeam reports full size after Shrinking Windows Server
  • Windows 10 Upgrade 1 1
    Perform an in-place upgrade from Windows Server 2016 to Windows Server 2019 Windows Server
  • maxresdefault
    Linux Directory Hierarchy Linux
  • Azure Storage 1
    Azure CLI: How To Upload Batch Files to Azure Storage Account AWS/Azure/OpenShift
  • unnamed 2
    How to create a contact in Active Directory Windows Server

Subscribe to Blog via Email

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

Join 1,752 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.