Configure local Gitlab based on Docker

Posted by MadnessRed on Fri, 18 Feb 2022 10:13:34 +0100

Technical background

Purchase discount www.fenfaw.com cn

Github and Gitee (code cloud) are the most common git based code hosting platforms. Now svn based code management warehouses are relatively rare, and most of them are internal code warehouses. However, based on the open source Gitlab, we can also immediately build a code hosting platform in the enterprise intranet, and integrate various code automatic testing tools to realize continuous integration and continuous testing.

As for the use of Docker container, we mainly want to avoid the installation problems caused by different OS distributions, and Gitlab official also provides Docker support. In other words, as long as there is Docker in the local environment, it is convenient to construct a local Gitlab environment. This article is also based on Docker to explain and show the configuration and use of local Gitlab.

Deployment of Gitlab environment based on Docker

For more usage and operation methods and use cases of docker, readers can refer to these blogs (blog 1, blog 2, blog 3, blog 4). Here we will directly use some functions of docker. For example, the first thing we need to do is to pull the image officially provided by Gitlab from dockerhub:

[dechin-manjaro gitlab]# docker pull gitlab/gitlab-ce

The download speed is good. After downloading, you can use docker images to check whether the relevant images are in the local warehouse list. It should be noted that these basic docker operations need root permission to execute. After downloading the image, find an empty directory locally and create several folders to bind the relevant directories in the container, so that the configuration files, log files and data files generated in the container can be synchronized to the local directory (not just the directory saved in the container).

[dechin-manjaro gitlab]# ll
 Total consumption 0
[dechin-manjaro gitlab]# mkdir -p config
[dechin-manjaro gitlab]# mkdir -p logs
[dechin-manjaro gitlab]# mkdir -p data

Here, the config, logs and data directories are created and bound to the / etc/gitlab, / var/log/gitlab and / var/opt/gitlab directories in the container with the - v instruction respectively. The specific operation instructions are as follows (refer to reference link 1):

[dechin-manjaro gitlab]# docker run -d -p 5443:443 -p 82:82 -p 2222:22 --name gitlab -v /home/dechin/projects/2021-softwares/gitlab/config:/etc/gitlab -v /home/dechin/projects/2021-softwares/gitlab/logs:/var/log/gitlab -v /home/dechin/projects/2021-softwares/gitlab/data:/var/opt/gitlab gitlab/gitlab-ce
c12732a4acfb4835b8d1353b62cf85868edc2466eb0669bbabc2059f7932c309

After execution, the container starts running in the background. We can see that the current running state is health: starting:

[dechin-manjaro gitlab]# docker ps -n 3
CONTAINER ID   IMAGE              COMMAND             CREATED          STATUS                            PORTS                                                                                                                       NAMES
c12732a4acfb   gitlab/gitlab-ce   "/assets/wrapper"   6 seconds ago    Up 5 seconds (health: starting)   80/tcp, 0.0.0.0:82->82/tcp, :::82->82/tcp, 0.0.0.0:2222->22/tcp, :::2222->22/tcp, 0.0.0.0:5443->443/tcp, :::5443->443/tcp   gitlab
36fc44248800   gitlab/gitlab-ce   "/assets/wrapper"   45 minutes ago   Exited (137) 5 minutes ago                                                                                                                                    hardcore_poitras
d9e431790dfa   gitlab/gitlab-ce   "--help"            45 minutes ago   Created                           22/tcp, 80/tcp, 443/tcp                                                                                                     intelligent_hopper

The next thing we need to do is to wait. After a period of time (about 2-3 minutes), we can check the running state of the container image again. At this time, we can find that the state has changed to health:

[dechin-manjaro gitlab]# docker ps -n 3
CONTAINER ID   IMAGE              COMMAND             CREATED          STATUS                        PORTS                                                                                                                       NAMES
c12732a4acfb   gitlab/gitlab-ce   "/assets/wrapper"   4 minutes ago    Up 4 minutes (healthy)
36fc44248800   gitlab/gitlab-ce   "/assets/wrapper"   49 minutes ago   Exited (137) 10 minuteitras
d9e431790dfa   gitlab/gitlab-ce   "--help"            49 minutes ago   Created               _hopper

This indicates that the initialization of the Gitlab container has been completed. Next, we can find Gitlab in the local directory (not the directory inside the container) The configuration file of Rb needs to be modified:

[dechin-manjaro gitlab]# vi config/gitlab.rb 

The main points to be modified are as follows:

external_url 'http://192.168.0.105: 82 '# about 32 lines. Pay attention to replacing the local ip address
gitlab_rails['gitlab_shell_ssh_port'] = 2222 # About line 631
# nginx['redirect_http_to_https_port'] = 82 # About 1312 lines
nginx['listen_port'] = 82 # About 1354

After the modification, restart the container image of gitlab (in the last run, we named it gitlab):

[dechin-manjaro gitlab]# docker restart gitlab
gitlab

As just now, you need to look at the container status to determine whether it is started successfully:

[dechin-manjaro gitlab]# docker ps -n 3
CONTAINER ID   IMAGE              COMMAND             CREATED          STATUS                
c12732a4acfb   gitlab/gitlab-ce   "/assets/wrapper"   13 minutes ago   Up 12 seconds (health:
36fc44248800   gitlab/gitlab-ce   "/assets/wrapper"   57 minutes ago   Exited (137) 18 minutere_poitras
d9e431790dfa   gitlab/gitlab-ce   "--help"            58 minutes ago   Created               igent_hopper

When the status changes to health, you can enter the next step:

[dechin-manjaro gitlab]# docker ps -n 3
CONTAINER ID   IMAGE              COMMAND             CREATED             STATUS             
c12732a4acfb   gitlab/gitlab-ce   "/assets/wrapper"   15 minutes ago      Up 2 minutes (healt
36fc44248800   gitlab/gitlab-ce   "/assets/wrapper"   About an hour ago   Exited (137) 20 min_poitras
d9e431790dfa   gitlab/gitlab-ce   "--help"            About an hour ago   Created            ent_hopper

Gitlab configuration

After the previous steps are successfully executed, we should be able to enter in the browser http://192.168.0.105:82/ (note to replace the local ip address here. Linux system can get it through ifconfig) open Gitlab's management interface as follows:

The first login will be required to reset the password, and the account is root. After resetting the password, you can enter Gitlab's login page:

After entering the root account and the password just set, you can log in to the Gitlab home page:

Click create project on the home page to create a local project:

After the project is created, the project home page is as follows:

Because we skipped a step here, when using Gitlab platform for the first time, we need to use RSA encryption to generate key pairs. Click Add ssh key on the interface just now to enter the interface of adding key pairs:

At this time, we need to execute the following instructions in the local terminal window to generate the key pair:

[dechin@dechin-manjaro projects]$ ssh-keygen -t rsa -b 2048
Generating public/private rsa key pair.
Enter file in which to save the key (/home/dechin/.ssh/id_rsa): private_gitlab.pub # File name, can not add pub
Enter passphrase (empty for no passphrase):  # Custom password input
Enter same passphrase again:  # Enter the custom password again
Your identification has been saved in private_gitlab.pub
Your public key has been saved in private_gitlab.pub.pub
The key fingerprint is:
SHA256:Xr3DhNi+8ZMJ×××××××××××××××××××0w dechin@dechin-manjaro
The key's randomart image is:
+---[RSA 2048]----+
|o..              |
|.. o. . .        |
|..+o + *         |
|   . E o.oo++o   |
|    o   . .++.   |
|     o..  . ..   |
+----[SHA256]-----+

After executing this instruction, two key files will be generated in the current directory:

[dechin@dechin-manjaro projects]$ ll
 Total consumption 8
-rw------- 1 dechin dechin 1876  5 May 16:43 private_gitlab.pub
-rw-r--r-- 1 dechin dechin  403  5 May 16:43 private_gitlab.pub.pub

One less pub file is the private key and should not be exposed, while one more pub file is the public key. You need to copy the string in this file and input it into the Gitlab configuration just now:

[dechin@dechin-manjaro projects]$ cat private_gitlab.pub.pub 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDt6VXrvTPhWr5iUy3KpIzRryX3SGBUAYietTMSqEOuZjRXr1u14lFk1cT5jwAHw7BtnfBOrwptTIYaWztoWR94gG1W1KFc6HRY0SWrUHtwXwOypWcqMv7Z2AT6TFIgNf/2ZZAlYisC0G9xTO5qTcYDyJe/88zcIm/5B6NQ7safAkGkGYY+WrFxBpnNU2bEdSbx4Sem2v2TD9GRxSg9RpLSXQaULi1bpDgGfxLJZBxj2Eeo11j9ayjipWFqJ43pJ dechin@dechin-manjaro

Generally, it is a string starting with SSH RSA. After entering, click Add Key to complete the key pair configuration:

Basic operation of code warehouse

After completing the basic configuration in the previous chapter, you can use https to clone the code warehouse to be managed for synchronous operation. First copy the warehouse link:

To execute the git clone command in the local terminal window:

[dechin@dechin-manjaro projects]$ git clone http://192.168.0.105:82/root/myfirstproject.git
 Cloning to 'myfirstproject'...
Username for 'http://192.168.0.105:82': root
Password for 'http://root@192.168.0.105:82': 
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
 In receiving object: 100% (3/3), complete.

After cloning, you can see a newly generated folder with the same name as the warehouse in the current directory. There is only one readme file initialized by the project, which is also the configuration selected when creating the project:

[dechin@dechin-manjaro projects]$ ll
 Total consumption 12
drwxr-xr-x 3 dechin dechin 4096  5 May 16:50 myfirstproject
-rw------- 1 dechin dechin 1876  5 May 16:43 private_gitlab.pub
-rw-r--r-- 1 dechin dechin  403  5 May 16:43 private_gitlab.pub.pub
[dechin@dechin-manjaro projects]$ cd myfirstproject/
[dechin@dechin-manjaro myfirstproject]$ ll
 Total consumption 4
-rw-r--r-- 1 dechin dechin 44  5 May 16:50 README.md

For more git related operations, readers can refer to this blog. Here we just want to show the use scenario demonstration of locally deployed gitlab:

[dechin@dechin-manjaro myfirstproject]$ mkdir src # Create an src directory
[dechin@dechin-manjaro myfirstproject]$ cd src/
[dechin@dechin-manjaro src]$ touch .gitkeep # Creating a gitkeep file can not simplify the directory and make the empty directory unrecognizable by git
[dechin@dechin-manjaro src]$ git status # View modification status
 In branch master
 Your branch and upstream branch 'origin/master' agreement.

Untracked files:
  (use "git add <file>..." (to include what to submit)
        ./

The submission is empty, but there are files that have not been tracked (using "git add" Establish tracking)
[dechin@dechin-manjaro src]$ git add ./ # Add all modifications under the current directory
[dechin@dechin-manjaro src]$ git commit -m 'Create a folder from localhost' Submit modification instructions
[master 211278b] Create a folder from localhost
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 src/.gitkeep
[dechin@dechin-manjaro src]$ git push # Push modification
Username for 'http://192.168.0.105:82': root # enter the user name of gitlab
Password for 'http://root@192.168.0.105:82 ': # enter the password of gitlab
 Enumeration object: 5, complete.
In object count: 100% (5/5), complete.
Compression using 8 threads
 Compressed objects: 100% (2/2), complete.
Write to object: 100% (4/4), 327 byte | 327.00 KiB/s, complete.
Total 4 (difference 0), reuse 0 (difference 0), packet reuse 0
To http://192.168.0.105:82/root/myfirstproject.git
   5ee2b12..211278b  master -> master

Comparison before and after submitting modification:


We can see that after the push is completed, an src directory appears on the Gitlab project interface, which indicates that the change is submitted successfully, and the description information of each change will be saved, and the latest change description will be displayed on the home page. Next, try Gitlab's branch management, create a new branch, and then submit directly:

[dechin@dechin-manjaro myfirstproject]$ git checkout -b dev
 Switch to a new branch 'dev'
[dechin@dechin-manjaro myfirstproject]$ git push
fatal: Current branch dev There is no corresponding upstream branch.
To push the current branch and establish a trace with the remote upstream, use

    git push --set-upstream origin dev

[dechin@dechin-manjaro myfirstproject]$ git push --set-upstream origin dev
Username for 'http://192.168.0.105:82': root
Password for 'http://root@192.168.0.105:82': 
Total 0 (difference 0), reuse 0 (difference 0), packet reuse 0
remote: 
remote: To create a merge request for dev, visit:
remote:   http://192.168.0.105:82/root/myfirstproject/-/merge_requests/new?merge_request%5Bsource_branch%5D=dev
remote: 
To http://192.168.0.105:82/root/myfirstproject.git
 * [new branch]      dev -> dev
 branch 'dev' Set to track from 'origin' Remote branch of 'dev'. 


After uploading a new branch, you will see different branches in the branch list. In addition to these functions of submitting and modifying synchronization, Gitlab also has a work management interface similar to Github, such as personal activity:

For example, the task submission and distribution system based on Merge Request and Issue:

Under the LAN of the same wireless network, terminal devices such as mobile phones can also access the private Gitlab warehouse:

Basically, the function is relatively perfect. The scheme of automation integration will be explained in the following blog about Jenkins.

Summary summary

According to the chronological order of the operation process, this paper introduces the Docker container deployment of Gitlab, the basic configuration of Gitlab platform and the basic management and synchronization method of code warehouse based on Git. By mastering this set of processes, we can build a code hosting system similar to Github locally, better manage the private code warehouse on the enterprise intranet, and avoid being stolen by other enterprises for code development on Github.

Copyright notice

The first link of this article is: https://www.cnblogs.com/dechinphy/p/gitlab.html
Author ID: DechinPhy
For more original articles, please refer to: https://www.cnblogs.com/dechinphy/

Reference link

  1. https://blog.csdn.net/shan165310175/article/details/92797199#commentBox