Build GitLab with Docker: Easy Deployment Guide(En)

Albert Weng
7 min readMar 7, 2024

Today, I’ll share an essential stage in the CI/CD process: Version Control System. Since my LAB environment requires a version control practice, I opted for the widely used Git system. Additionally, to establish a dedicated environment locally, I considered using GitLab.

This article will cover the following topics:

  1. GitLab v.s GitHub
  2. Basic Overview of GitLab
  3. Deployment Using Docker
  4. Post-Installation: MTA Configuration
  5. Conclusion

1. GitLab v.s GitHub

I guess this is the most commonly asked first question…Which one should I use, GitLab or GitHub?

First of all, whether it’s GitLab or GitHub, it boils down to a version control system built on Git. Git was originally designed by Linus Torvalds for the Linux Kernel. While initially created for Linux, its ease of use led many open-source projects to adopt this method for management.

For those who might not be clear, let’s briefly explain what they are:

(1) GitLab: It’s an alternative to GitHub, also based on Git. Designed for hosting open-source projects, its purpose is to make developers’ lives easier. Different from the latter, GitLab not only provides repository management and version control but also offers Wiki hosting and an issue tracking system. It can be used to create and manage various projects.

(2) GitHub: A collaborative development platform with web services and a version control system. This platform focuses on collaboration among developers to propagate and support their software, although many also use it for purposes beyond software development. The platform is written in Ruby on Rails. GitHub hosts a vast amount of publicly accessible open-source projects. Its value is evident in Microsoft’s decision to acquire the platform in 2018, contributing a figure not less than 7.5 billion USD.

Next are the advantages of these two systems:

(1) GitLab:

  • Unlimited on the free plan, although it has paid plans.
  • Open source.
  • Allows self-hosting on any plan.
  • Integrates well with Git

(2) GitHub:

  • Free service, although it also offers paid services.
  • Very fast search capabilities.
  • Large community, easy to find help.
  • Provides practical tools that collaborate well with Git.
  • Easy integration with other third-party services.
  • Can also be used with TFS, HG, and SVN.

Of course, they each have their own drawbacks. For example, GitHub requires a paid account if the usage exceeds 1GB, while GitLab may not have the same extensive resources as GitHub but offers the benefit of complete control.

2. Basic Overview of GitLab

The above briefly explained the differences from GitHub. This section will specifically elaborate on GitLab.

GitLab is now divided into GitLab CE (Community Edition) and GitLab EE (Enterprise Edition). The core code is the same. If you don’t want to set up GitLab yourself, you can use the gitlab.com service directly.

Common features include:

  • Git version control system.
  • CI/CD Pipeline
  • Automatically builds based on specific YAML files stored by users in different projects.
  • It smoothly integrates common tools from testing to deployment.
  • Provides an integrated development workflow: GitLab Workflow.
  • CI/CD Pipeline jobs must be executed with the assistance of GitLab Runner.
  • Code project management features (e.g., Wiki, Issue Tracking, Kanban).

For related configurations, log file locations, and data file storage locations, additional directories need to be created, which can be within the home directory or separately.

※ The locations and descriptions used by the GitLab container are as follows:

※ GitLab User Permissions

3. Deployment Using Docker

Next, we will proceed to the deployment phase. You can try deploying GitLab based on the provided instructions. GitLab offers various deployment methods, and you can refer to the official documentation for more details.

We will use a Docker image for deployment, and this image includes all the necessary components. There are three main installation methods:

1. Docker Engine
2. Docker Compose
3. Docker Swarm

In this lab, we will use Docker Compose for deployment. Please note that the official Docker image does not include an MTA (Mail Transfer Agent) component. If you need email functionality, it is recommended to set up a separate container or service to handle it.

For Kubernetes (K8S) environments, it is recommended to use the official deployment method (Operator) to avoid potential single-point failures.

In this example, we will use the image: gitlab/gitlab-ce:latest.

#---------------------------------------------------
# S3-1. Create directory
#---------------------------------------------------
[root]# mkdir -p /srv/gitlab
[root]# vim /root/.bash_profile
export GITLAB_HOME=/srv/gitlab
#---------------------------------------------------
# S3-2. Change the GitLab Shell SSH port
#---------------------------------------------------
=> if using docker-compose, modify the configuration.

※ GitLab uses SSH for communication with Git, and the default port is 22.
However, this may conflict with the host machine's SSH configuration.
You can choose to either directly change the host's SSH port and avoid using 22,
or modify GitLab to use a different port than 22.
#---------------------------------------------------
# S3-3. create docker-compose.yaml
#---------------------------------------------------
[root]# mkdir -p /root/gitlab/
[root]# vim docker-compose.yml
version: '3.6'
services:
gitlab:
image: gitlab/gitlab-ce:latest
container_name: gitlab
restart: always
hostname: 'gitlab.test.example.poc'
environment:
GITLAB_OMNIBUS_CONFIG: |
external_url 'http://gitlab.test.example.poc:8929'
gitlab_rails['gitlab_shell_ssh_port'] = 2424
ports:
- '8929:8929'
- '8443:8443'
- '2424:2424'
volumes:
- '$GITLAB_HOME/config:/etc/gitlab'
- '$GITLAB_HOME/logs:/var/log/gitlab'
- '$GITLAB_HOME/data:/var/opt/gitlab'
shm_size: '256m'

[root]# docker-compose up -d
[root]# docker ps
#---------------------------------------------------
# S3-4. Logon Web
#---------------------------------------------------
http://gitlab.test.example.poc:8929
=> modify initial password (root/P@ssw0rd)

If it doesn't appear, you can retrieve the login password from $GITLAB_HOME/config/initial_root_password.
[root]# docker exec -it gitlab /bin/bash (cat $GITLAB_HOME/config/initial_root_password)
Password: I+UkaZl6xT0iMBBXJ3TIZNor+M+ZnOz//Hhoz01zSHk=

4. Post-Installation: MTA Configuration

Here are several tasks typically performed after installing GitLab, and you can configure them based on your needs:

- Email and Notification
- CI/CD
- Security
- Authentication
- Backup and Upgrade

This article will focus on configuring Email and Notification, using the local SMTP server for sending emails.

#-------------------------------------------------
# S4-1. Obtain docker host ip
#-------------------------------------------------
[root]# ip a
10.107.88.7
#-------------------------------------------------
# S4-2. Edit docker-compose, Add host ip
#-------------------------------------------------
[root]# vim docker-compose.yaml
version: '3.6'
services:
gitlab:
image: gitlab/gitlab-ce:latest
extra_hosts:
- "dockerhost:10.107.88.7"
container_name: gitlab
restart: always

[root]# docker-compose up -d
#-------------------------------------------------
# S4-3. Confirm container /etc/hosts
#-------------------------------------------------
[root]# docker exec -it gitlab /bin/bash
root# cat /etc/hosts
10.107.88.7 dockerhost
172.18.0.2 gitlab.test.example.poc gitlab
#-------------------------------------------------
# S4-4. Edit sendmail
#-------------------------------------------------
[root]# yum install sendmail
[root]# vim /etc/mail/sendmail.cf (Open 25 Port)
#Comment
#O DaemonPortOptions=Port=smtp,Addr=127.0.0.1, Name=MTA
#add
O DaemonPortOptions=Port=smtp,Addr=0.0.0.0, Name=MTA

[root]# systemctl restart sendmail
[root]# nc -v localhost 25s
#-------------------------------------------------
# S4-5. Allow relay
#-------------------------------------------------
[root]# vim /etc/mail/access
add
Connect:10.107.88 RELAY
#-------------------------------------------------
# S4-6. GitLab sends emails through the host's Sendmail
#-------------------------------------------------
[root]# vim /srv/gitlab/config/gitlab.rb
gitlab_rails[‘smtp_enable’] = true
gitlab_rails[‘smtp_address’] = “dockerhost"
gitlab_rails[‘smtp_port’] = 25
#gitlab_rails[‘smtp_user_name’] = “"
#gitlab_rails[‘smtp_password’] = “"
gitlab_rails[‘smtp_domain’] = “dockerhost"
#gitlab_rails[‘smtp_authentication’] = “none"
gitlab_rails[‘smtp_openssl_verify_mode’] = ‘none’
gitlab_rails[‘smtp_enable_starttls_auto’] = false
gitlab_rails[‘smtp_tls’] = false
gitlab_rails[‘smtp_ssl’] = false
gitlab_rails[‘smtp_force_ssl’] = false
#gitlab_rails[‘smtp_pool’] = false

Set Sender
gitlab_rails[‘gitlab_email_from’] = ‘gitlabuser@test.example.poc’
gitlab_rails[‘gitlab_email_display_name’] = ‘gitlabuser’

Confit gitlab admin
alertmanager[‘admin_email’] = ‘c’

[root]# docker exec -it gitlab /bin/bash
root# gitlab-ctl reconfigure

[2024-02-16T03:19:10+00:00] INFO: Report handlers complete
Infra Phase complete, 7/778 resources updated in 55 seconds
gitlab Reconfigured!
#-------------------------------------------------
# S4-7. This is a test to confirm that GitLab can send an email
# from the GitLab container to localhost
#-------------------------------------------------
[root]# nc -v localhost 25
EHLO localhost (Say hello)
MAIL FROM:<gitlabuser@test.example.poc>
RCPT TO:<gitlabuser@test.example.poc>
DATA
SUBJECT: mail server text!
Test!!

5. Conclusion

Today, we discussed the differences between GitLab and GitHub and shared the process of deploying GitLab in a local environment for testing. In practice, both GitLab and GitHub are used by the author. GitHub primarily serves as an offsite backup for test articles, and the GitHub app allows the author to conveniently access notes using a mobile phone at any time.

In the future, we will continue to share practical approaches such as setting up CI/CD pipelines and deploying on Kubernetes after installation. We hope for your continued interest.

Finally, we appreciate your support, and if you’d like to stay updated on upcoming articles, please follow us, goodbye!

※ References:

--

--

Albert Weng

You don't have to be great to start, but you have to start to be great