How to Setup and Run Your GitLab CI/CD job in docker containers

What is GitLab CI/CD?

GitLab CI/CD is a tool built into GitLab for software development through the CI(Continuous Integration),CD(Continuous deployment) and CD(Continuous Delivery).GitLab CI/CD provides git repository management, issue tracking, code reviews, wikis, and activity feeds and can be install on on-premise and connect it with Active Directory and LDAP servers for secure authorization and authentication.

Core Features:

  1. Easy Installation and configuration : It is Very Easy to setup and install GitLab CI/CD and official documentation contains all the necessary steps.

2. Auto-Scaling CI Runners : GitLab Runner can autoscale, so that your infrastructure contains only as many build instances as are necessary at any time.

3. Issue Tracking : Because of great issue tracking features, GitLab is a preferred CI/CD tool for numerous open-source projects.For simple and trouble free monitoring,testing results are displayed over GitLab UI.

4. Active Community Support : Active community is one of the plus point of the GitLab CI/CD.

5. Milestone Setting : It is a way to track issues and merge requests created to achieve a broader goal in a certain period of time.You can easily assign project milestones to any issue.

GitLab CI/CD flow

  1. Once you push the Code into the GitLab Repository then Gitlab will automatically Trigger CI/CD Process.
  2. You can configure GitLab CI/CD by creating a file called .gitlab-ci.yml in the root directory of the project. This file automatically runs whenever you push a commit to the server and then it processes the series of tasks you specified. This file can have single or multiple stages that run one after the other(in series).
  3. Each stage can consist of multiple jobs that are executed in parallel by the gitlab-runner. Using .gitlab-ci.yaml file where we have defined actual all the CI/CD steps, Gitlab-runner will perform the action(Build -> Test ->Release ->Deploy).

In This article You will see how to setup and configure GitLab CI/CD.

Prerequisite :

  1. You should have created a account on the GitLab and have maintainer/Owner access on the repository where you have .gitlab-ci.yml file.
  2. docker should be installed on the machine.

Step 1: Create .gitlab-ci.yml in the root directory of the Repo.

Let us take a simple example of .gitlab-ci.yml file for building and testing node JS Application and let us understand the below .gitlab-ci.yml file.

image: node:lateststages:
- build
- test
- node_modules/
- apt-get update -qq && apt-get install
stage: build
- npm install
- ci
- node_modules/
stage: test
script: npm test
- ci

Breaking down the file:

image: node:latest

We are taking node js base image from the docker Hub.If your project in Python,Java, Go, PHP, etc. specify the correct image from the Docker Hub or from Your Artifactory Repository.

- build
- test

Using this GitLab will run build stage first and then test Stage. You can define number of stages with sequence over here.

- node_modules/

This creates a temporary cache folder that prevents from the node_modules to recreated each CI run.

- apt-get update -qq && apt-get install

The before_script tells GitLab to run whatever you have specified before doing anything else.You can consider this as a init stage.

- ci

You can also specify tags to the Gitlab-Runner. Based on the tags which we have defined in the stage, Gitlab will pick matching tag Gitlab-Runner executor to perform task.If You haven’t Specify the tag then GitLab will assign Random GitLab-Runner with no tags.

Then it will read each of stage sequentially and execute the steps.

Step 2 : Setup GitLab Runner

What is GitLab Runner?

GitLab Runner is an application that works with GitLab CI/CD to run jobs in a pipeline. You can choose to install the GitLab Runner application on infrastructure that you own or manage. GitLab Runner can be installed and used on GNU/Linux, macOS, FreeBSD , Windows etc and also on the docker container.

I have installed GitLab Runner inside the docker container. Run the below command to start GitLab Runner docker container and register it Your Gitlab Account. Then it will ask Gitlab Url,Token (Copy the Registration Token from Runners section of Settings / CI/CD page in the project),desciption,tag etc.

Before Running the below command,disable the shared Runner(highlighted below) and Copy the token from Specific Runners section from left hand side.

[root@control-plane var]# docker run -i -t --name docker-gitlab-runner001 --restart always   -v /var/run/docker.sock:/var/run/docker.sock   -v /srv/gitlab-runner/config
:/etc/gitlab-runner gitlab/gitlab-runner:latest register

Runtime platform arch=amd64 os=linux pid=6 revision=c1edb478 version=14.0.1
Running in system-mode.
Enter the GitLab instance URL (for example,
Enter the registration token:
<Enter your token>
Enter a description for the runner:
[ddd1a78e3458]: docker
Enter tags for the runner (comma-separated):
Registering runner... succeeded runner=DHFXdETW
Enter an executor: custom, docker-ssh, parallels, ssh, kubernetes, docker, shell, virtualbox, docker+machine, docker-ssh+machine:
Enter the default Docker image (for example, ruby:2.6):
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!

Once done You can see that above command will start one docker container executor and You can see Your container executors inside Settings ->CI/CD ->Runners into Your Gitlab Project(as shown in below image).

Now CI/CD Setup is done. Once You Push your code to Your Repository then GitLab will automatically Trigger CI/CD Process. You can monitored it by Visiting CICD -> Pipeline Option.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Akshay Bobade

Akshay Bobade

I have total 3 Plus years of experience as a Devops engineer and currently dealing with Cloud, Containers, Kubernates and Bigdata technologies.