How to Create Gitlab Runner Container on Docker (Tutorial)

How to Create Gitlab Runner Container on Docker (Tutorial). In this post, we will introduce GitLab, its advantages, then explain how to create GitLab runner container on Docker.

What is GitLab?

Image Source: Stackoverflow

Gitlab is an open source repository management system for git, which has extensive features like code reviews, issue tracking, etc. All in all, Gitlab is a collaborative tool that helps manage repositories hosted on servers. Also provides issues tracking in the CI/CD pipeline functions using an open source license.

Initially, the repository was develop by Valery Sizov and Dmitry Zaporozhets which began as a project in 2011. Over time, the tool has become one of the popular platforms. Offers end to end DevOps capabilities and supports the software development lifecycle.

Today, many DevOps use the tool for the software development process. Free tool, written in Ruby and Go language, and is simple to manage. Additionally, you can set the tool in a few minutes.

Cisco, Juniper, and IBM Sony are a few high profile tech companies that use Gitlab to deploy and collaborate web based projects. Well, the GitLab project came into practice in 2011 to make the lives of developers easier. Using GitLab, DevOps create, store, deploy, and track private and public projects. Public repository generally helps in issue tracking and documentation.

Altogether, the platform further offers features like project management, bug tracking, and project analysis. Users view the project development insights and complete the development cycle on GitLab.

In GitLab, one can also pull requests from other developers and provide comments on their code. Basically, GitLab also allows us to import repositories from Google Code, Bitbucket, and others.

Features of GitLab

If the organization is just starting, the GitLab Free Plan offers a decent selection of features. On the other hand, the premium plan also gives managers access to project management and code integrity checks. Let us have a look at GitLab’s key features:

  • Customizable Templates.
  • Access control.
  • User Permission.
  • Automated code testing.
  • Merge, assign, and closure sensitive issues.
  • Support Multiple Kubernetes cluster.
  • Uses third party servers to manage docker images.

Advantages of GitLab

With GitLab, many organizations can improve their performance and deliver better results in less time. Cloud native application that is faster and more efficient than other software and app. Here are a few more benefits of using GitLab:

Easy to Configure

Developers find it easy to install the GitLab CI/CD tool. Well, it offers a private repository with limited access. Further, users can configure it in a container, on premises, or in a cloud. All you need to keep in mind is configuration requires you to create a virtual machine. Also requires a stick to pre-set configuration. Knowledge of Git on the command line can also help a user configure GitLab projects.

Deployment scheduling

To avoid conflicts, IT teams require their staff to perform constant monitoring of the performance as well as scheduling. But, it was a consuming task. As a result, with the introduction of GitLab, users could easily run CI/CD pipeline schedules. All you need to do is just specify a time for deployment management. This feature also immediately alerts you on noticing changes to the code.

Automates Building, Deploying, and Testing of codes in pipelines

Besides, Auto DevOps feature helps in building, testing, deploying, and tracking applications and their codes automatically via the CI/CD pipeline. Not only it saves time but also helps enforce standard practices. In order to take maximum benefit of the Auto DevOps feature, it is essential to integrate a Kubernetes cluster.

Generates maturity feedback

Consequently, GitLab offers Score boards that brief if developers are making appropriate use of GitLab features. Equally, it also helps in fixing bugs prior to the commencement of the process as well as shipping the right software. Further, using this feature, the teams measure which areas require an extension. Additionally, it helps to compare the activities with other organizations.

Source Code Security

Another benefit of GitLab is it offers various security measures for CI/CD pipelines. With the control over live codes, granular restrictions, and user permissions for Kerberos. Not to forget, a fully distributed version control system that allows developers to maintain, control and manage the code storage location. For example, the administration has full access to control and manage the server and location of codes. Also, they have the authority to store external codes.

Up next with How to Create Gitlab Runner Container on Docker (Tutorial) is to get to the main part of the article topic. Please read on. 

How to Create Gitlab Runner Container on Docker (Tutorial)

In this step by step guide, we will show you how to create a Gitlab runner container using Docker and Docker Compose on Ubuntu 20.04 server.

Prerequisites

  • A server running Ubuntu 20.04.
  • A root user or a user with sudo privileges.

Getting Started

First, you will need to update and upgrade all the system packages to the latest version. Update all of them by running the following command:

				
					apt update -y
apt upgrade -y
				
			

Once all the packages are updated, install other required dependencies using the following command:

				
					apt install apt-transport-https ca-certificates curl software-properties-common -y
				
			

Next, having all the dependencies installed, please proceed to install Docker and Docker Compose.

Install Docker and Docker Compose

By default, the latest version of Docker and Docker Compose is not included in the Ubuntu default repository. So you will need to install both packages from the Docker’s official repository.

First of all, download and add the Docker GPG key using the following command:

				
					curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -
				
			

Next, add the Docker repository to APT with the following command:

				
					add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable"
				
			

Forthwith, once the Docker repository is added to the APT, you see all available Docker versions using the following command:

				
					apt-cache policy docker-ce
				
			

You should see the following output:

				
					docker-ce:
  Installed: (none)
  Candidate: 5:20.10.21~3-0~ubuntu-focal
  Version table:
     5:20.10.21~3-0~ubuntu-focal 500
        500 https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
     5:20.10.20~3-0~ubuntu-focal 500
        500 https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
     5:20.10.19~3-0~ubuntu-focal 500
        500 https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
     5:20.10.18~3-0~ubuntu-focal 500
        500 https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
     5:20.10.17~3-0~ubuntu-focal 500
        500 https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
     5:20.10.16~3-0~ubuntu-focal 500
        500 https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
     5:20.10.15~3-0~ubuntu-focal 500
        500 https://download.docker.com/linux/ubuntu focal/stable amd64 Packages

				
			

Then, please install both Docker and Docker Compose with the following command:

				
					apt install docker-ce docker-compose -y
				
			

After the successful installation, please verify the status of Docker with the following command:

				
					systemctl status docker
				
			

See the Docker active status in the following output:

				
					● docker.service - Docker Application Container Engine
     Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
     Active: active (running) since Mon 2022-11-14 15:32:15 UTC; 18s ago
TriggeredBy: ● docker.socket
       Docs: https://docs.docker.com
   Main PID: 3746 (dockerd)
      Tasks: 9
     Memory: 23.0M
     CGroup: /system.slice/docker.service
             └─3746 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

Nov 14 15:32:15 ubuntu dockerd[3746]: time="2022-11-14T15:32:15.162040041Z" level=warning msg="Your kernel does not support CPU realtime sche>
Nov 14 15:32:15 ubuntu dockerd[3746]: time="2022-11-14T15:32:15.162060173Z" level=warning msg="Your kernel does not support cgroup blkio weig>
Nov 14 15:32:15 ubuntu dockerd[3746]: time="2022-11-14T15:32:15.162076490Z" level=warning msg="Your kernel does not support cgroup blkio weig>
Nov 14 15:32:15 ubuntu dockerd[3746]: time="2022-11-14T15:32:15.162756715Z" level=info msg="Loading containers: start."
Nov 14 15:32:15 ubuntu dockerd[3746]: time="2022-11-14T15:32:15.366945583Z" level=info msg="Default bridge (docker0) is assigned with an IP a>
Nov 14 15:32:15 ubuntu dockerd[3746]: time="2022-11-14T15:32:15.456090226Z" level=info msg="Loading containers: done."
Nov 14 15:32:15 ubuntu dockerd[3746]: time="2022-11-14T15:32:15.481585172Z" level=info msg="Docker daemon" commit=3056208 graphdriver(s)=over>
Nov 14 15:32:15 ubuntu dockerd[3746]: time="2022-11-14T15:32:15.481755844Z" level=info msg="Daemon has completed initialization"
Nov 14 15:32:15 ubuntu systemd[1]: Started Docker Application Container Engine.

				
			

Additionally, you can also verify the Docker version using the following command:

				
					docker --version
				
			

You should see the Docker version in the following output:

				
					Docker version 20.10.21, build baeda1f
				
			

Obtain a Runner Token From GitLab

Before starting, you will need to create a group in GitLab and obtain a token to run the GitLab Runner. Step one:

 

1.Login to the GitLab web interface. You should see the following page:

2. Click on the + sign on the top screen then click on the New group. You should see the following page:

3. After that, click on the Create group. You should see the following page:

4. Provide your group name, other informations and click on the Create group. You should see the following page:

5. Click on the Settings => CI/CD. You should see the following page:

6. Collapse the Runner section, disable shared runner for this group and click on the Take me there!. You should see the following page:

7. Click on the Register a group runner and copy the Registration token to safe place. You will require this token to register the Docker container in the next step.

Create a GitLab Runner Container

In this section, we will create a docker-compose file to launch the GitLab runner container then register it using the token obtained from the GitLab.

Create a directory to hold all configuration files:

				
					mkdir gitlab-runner
				
			

Navigate to the gitlab-runner container and create another directory:

				
					cd gitlab-runner
mkdir config
				
			

Then, create a config.toml configuration file using the following command:

				
					nano config/config.toml
				
			

Add the following configurations:

				
					concurrent = 1
check_interval = 0

[session_server]
  session_timeout = 1800

[[runners]]
  name = "Sample Runner 1"
  url = "https://gitlab.com/"
  token = "GR1348941-EBAabCWp8KbuZ6PG5BS"
  executor = "docker"
  [runners.custom_build_dir]
  [runners.cache]
    [runners.cache.s3]
    [runners.cache.gcs]
    [runners.cache.azure]
  [runners.docker]
    tls_verify = false
    image = "docker:stable"
    privileged = false
    disable_entrypoint_overwrite = false
    oom_kill_disable = false
    disable_cache = false
    volumes = ["https://net.cloudinfrastructureservices.co.uk/var/run/docker.sock:/var/run/docker.sock", "/cache"]
    shm_size = 0

				
			

Save and close the file when you are done. Then, create a docker-compose.yml file using the following command:

				
					nano docker-compose.yml
				
			

Add the following lines:

				
					version: '3'

services:
  gitlab-runner-container:
    image: gitlab/gitlab-runner:v14.3.2
    container_name: gitlab-runner-container
    restart: always
    volumes:
      - ./config/:/etc/gitlab-runner/
      - /var/run/docker.sock:/var/run/docker.sock

				
			

Here you save and close the file then run the following command to start the GitLab runner container.

				
					docker-compose up -d
				
			

You should see the following output:

				
					Creating network "gitlab-runner_default" with the default driver
Pulling gitlab-runner-container (gitlab/gitlab-runner:v14.3.2)...
v14.3.2: Pulling from gitlab/gitlab-runner
35807b77a593: Pull complete
3b9cc5728056: Pull complete
8a9db090e379: Pull complete
eaaf4572c9c3: Pull complete
a62624e91cfd: Pull complete
Digest: sha256:4cc075aa6a0142fd31cbeb2cfbcba9c126fa3c2e300608a3a34ea40229aee916
Status: Downloaded newer image for gitlab/gitlab-runner:v14.3.2
Creating gitlab-runner-container ... done

				
			

Please verify the Docker image downloaded by the above command using the following command:

				
					docker images
				
			

You will get the following output:

				
					REPOSITORY             TAG       IMAGE ID       CREATED         SIZE
gitlab/gitlab-runner   v14.3.2   0a20d405c892   13 months ago   2.53GB
				
			

Additionally, can also check the status of the running container using the following command:

				
					docker ps
				
			

See the running container in the following output:

				
					CONTAINER ID   IMAGE                          COMMAND                  CREATED          STATUS          PORTS     NAMES
32fb2415a027   gitlab/gitlab-runner:v14.3.2   "/usr/bin/dumb-init …"   15 seconds ago   Up 13 seconds             gitlab-runner-container

				
			

Next, run the following command to register a new runner:

				
					docker-compose exec gitlab-runner-container \
    gitlab-runner register \
    --non-interactive \
    --url https://gitlab.com \
    --registration-token GR1348941-EBAabCWp8KbuZ6PG5BS \
    --executor docker \
    --description "Sample Runner 1" \
    --docker-image "docker:stable" \
    --docker-volumes /var/run/docker.sock:/var/run/docker.sock
				
			

Once the container is successfully registered to the GitLab. You will get the following output:

				
					Runtime platform                                    arch=amd64 os=linux pid=18 revision=e0218c92 version=14.3.2
Running in system-mode.                            
                                                   
Registering runner... succeeded                     runner=GR134894
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded! 
				
			

Verify Registered GitLab Runner Container

At this point, the GitLab runner container is started and registered with GitLab. You can now visit the GitLab and verify the registered runner in your group’s “CI/CD Settings”:

Thank you for reading How to Create Gitlab Runner Container on Docker (Tutorial). We shall conclude. 

How to Create Gitlab Runner Container on Docker (Tutorial) Conclusion

Summarizing, this post was about installing Docker and Docker Compose, obtaining a token from the GitLab, creating a GitLab runner container then verify it from the GitLab web interface. You can now use the GitLab runner to run the jobs over multiple machines and send the results to GitLab and which can be placed on separate users, servers, and local machine.

Overall, GitLab helps push your repositories, support collaboration, enable keeping track of development work, display projects, and more. Moreover, it is a collaborative tool with extensive features like issue tracking, task feeds, code reviews, etc. Users optimize the collaborative workflows to execute a software development lifecycle with GitLab. Have a look at its above listed features and advantages to better understand GitLab.

Have a look at more GitLab section in our blog over here

Avatar for Hitesh Jethva
Hitesh Jethva

I am a fan of open source technology and have more than 10 years of experience working with Linux and Open Source technologies. I am one of the Linux technical writers for Cloud Infrastructure Services.

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x