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
  • runlevels
    How to check your current Runlevel in Linux Linux
  • Veeam
    Failed to connect to Veeam Backup and Replication server: Remote Channel Sink UriNotPublished, remote connection refused, and failed to start the service Backup
  • cimInstance wmi
    How to fix Get-CimInstance Access PermissionDenied Windows
  • RUST FEATURE
    How to install Rust in a Linux System Linux
  • scroll
    How to Take a Scrolling Screenshot on Windows 11 Windows
  • banner
    How to Secure your API key in Postman Password Manager
  • git default branch min
    Git config –global init.defaultBranch: Error cannot lock ref ‘refs/remotes/origin/windows’, not a directory Version Control System
  • sql
    How to alter a DATABASE compatibility level Oracle/MSSQL/MySQL

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.

  • Click to share on X (Opens in new window) X
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Pinterest (Opens in new window) Pinterest
  • Click to share on Tumblr (Opens in new window) Tumblr
  • Click to share on Telegram (Opens in new window) Telegram
  • Click to share on WhatsApp (Opens in new window) WhatsApp
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on Bluesky (Opens in new window) Bluesky
  • Click to share on Threads (Opens in new window) Threads
  • Click to 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

  • Opswork blog 1440x800 1
    AWS Opsworks For Chef Automate Configuration Management Tool
  • db nginxseriesanisibleplaybook 1540x748 1
    How to install and configure Ansible on Ubuntu Configuration Management Tool
  • How to download install and use Kitty SSH Client on Windows
    How to Download and Use KiTTY SSH Client on Windows Configuration Management Tool
  • Installing Jenkins
    How to Install and Configure Jenkins on Windows 11 Configuration Management Tool
  • ansible logo600 591x296 1 1
    Ansible error: Server unreachable, ssl: auth method ssl requires a password Configuration Management Tool
  • powershell01 1
    Create a self-signed certificate and export it in PFX format via PowerShell [Part 1] Scripts

More Related Articles

Opswork blog 1440x800 1 AWS Opsworks For Chef Automate Configuration Management Tool
db nginxseriesanisibleplaybook 1540x748 1 How to install and configure Ansible on Ubuntu Configuration Management Tool
How to download install and use Kitty SSH Client on Windows How to Download and Use KiTTY SSH Client on Windows Configuration Management Tool
Installing Jenkins How to Install and Configure Jenkins on Windows 11 Configuration Management Tool
ansible logo600 591x296 1 1 Ansible error: Server unreachable, ssl: auth method ssl requires a password Configuration Management Tool
powershell01 1 Create a self-signed certificate and export it in PFX format via PowerShell [Part 1] Scripts

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

sysadmin top30a

  • runlevels
    How to check your current Runlevel in Linux Linux
  • Veeam
    Failed to connect to Veeam Backup and Replication server: Remote Channel Sink UriNotPublished, remote connection refused, and failed to start the service Backup
  • cimInstance wmi
    How to fix Get-CimInstance Access PermissionDenied Windows
  • RUST FEATURE
    How to install Rust in a Linux System Linux
  • scroll
    How to Take a Scrolling Screenshot on Windows 11 Windows
  • banner
    How to Secure your API key in Postman Password Manager
  • git default branch min
    Git config –global init.defaultBranch: Error cannot lock ref ‘refs/remotes/origin/windows’, not a directory Version Control System
  • sql
    How to alter a DATABASE compatibility level Oracle/MSSQL/MySQL

Subscribe to Blog via Email

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

Join 1,841 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

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