Docker搭建私有Gitlab

gitlab

GitLab is the only place where enterprises build mission‑critical software.

安装

创建目录

1
2
3
mkdir /root/gitlab/config/ssl
mkdir /root/gitlab/logs
mkdir /root/gitlab/data

阿里云申请ssl证书,下载Nginx版本

解压到/root/gitlab/config/ssl目录

pem后缀改成crt

image-20230627171654621

image-20230707154229073

创建docker-compose.yaml文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
version: '3'
services:
gitlab:
environment:
GITLAB_OMNIBUS_CONFIG: |
external_url 'https://gitlab.jonty.top'
nginx['listen_port'] = 443
nginx['redirect_http_to_https'] = true
nginx['ssl_certificate'] = "/etc/gitlab/ssl/gitlab.jonty.top.crt"
nginx['ssl_certificate_key'] = "/etc/gitlab/ssl/gitlab.jonty.top.key"

gitlab_rails['smtp_enable'] = true
gitlab_rails['smtp_address'] = "smtp.qq.com"
gitlab_rails['smtp_port'] = 465
gitlab_rails['smtp_user_name'] = "501211312@qq.com"
gitlab_rails['smtp_password'] = "your_passwork_key"
gitlab_rails['smtp_authentication'] = "login"
gitlab_rails['smtp_enable_starttls_auto'] = false
gitlab_rails['smtp_tls'] = true
gitlab_rails['gitlab_email_from'] = 'gitlab.jonty.top'
gitlab_rails['smtp_domain'] = "smtp.qq.com"


container_name: gitlab
ports:
- 80:80
- 443:443
image: gitlab/gitlab-ee:latest
restart: always
volumes:
- /root/gitlab/config:/etc/gitlab
- /root/gitlab/logs:/var/log/gitlab
- /root/gitlab/data:/var/opt/gitlab
networks:
- internal-network

networks:
internal-network:
external: true

启动

1
2
3
4
5
docker compose up -d

[root@VM-0-5-centos gitlab]# docker compose up -d
[+] Running 1/1
✔ Container gitlab Started

进入容器查看初始密码,账号为:root

1
2
docker exec -it gitlab /bin/bash
cat /etc/gitlab/initial_root_password

image-20230707155449732

image-20230707155527280

生成许可证

安装Ruby:如何在Ubuntu 20.04上安装Ruby2.7.0 - GlaryJoker - 博客园 (cnblogs.com)

安装Ruby

版本大于2.7

1
2
3
4
5
6
sudo apt update
sudo apt install ruby-full

# 查看版本
root@gitlab:/home/jonty/gitlab# ruby -v
ruby 2.7.0p0 (2019-12-25 revision 647ee6f091) [x86_64-linux-gnu]

安装gitlab-license

1
gem install gitlab-license

创建目录

1
mkdir /root/gitlab/license & cd

创建license.rb文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
require "openssl"
require "gitlab/license"
key_pair = OpenSSL::PKey::RSA.generate(2048)
File.open("license_key", "w") { |f| f.write(key_pair.to_pem) }
public_key = key_pair.public_key
File.open("license_key.pub", "w") { |f| f.write(public_key.to_pem) }
private_key = OpenSSL::PKey::RSA.new File.read("license_key")
Gitlab::License.encryption_key = private_key
license = Gitlab::License.new
license.licensee = {
"Name" => "Jonty",
"Company" => "Jonty.tEd",
"Email" => "gitlab@jonty.top",
}
license.starts_at = Date.new(2021, 1, 1) # 开始时间
license.expires_at = Date.new(2050, 1, 1) # 结束时间
license.notify_admins_at = Date.new(2049, 12, 1)
license.notify_users_at = Date.new(2049, 12, 1)
license.block_changes_at = Date.new(2050, 1, 1)
license.restrictions = {
active_user_count: 10000,
}
puts "License:"
puts license
data = license.export
puts "Exported license:"
puts data
File.open("GitLabBV.gitlab-license", "w") { |f| f.write(data) }
public_key = OpenSSL::PKey::RSA.new File.read("license_key.pub")
Gitlab::License.encryption_key = public_key
data = File.read("GitLabBV.gitlab-license")
$license = Gitlab::License.import(data)
puts "Imported license:"
puts $license
unless $license
raise "The license is invalid."
end
if $license.restricted?(:active_user_count)
active_user_count = 10000
if active_user_count > $license.restrictions[:active_user_count]
raise "The active user count exceeds the allowed amount!"
end
end
if $license.notify_admins?
puts "The license is due to expire on #{$license.expires_at}."
end
if $license.notify_users?
puts "The license is due to expire on #{$license.expires_at}."
end
module Gitlab
class GitAccess
def check(cmd, changes = nil)
if $license.block_changes?
return build_status_object(false, "License expired")
end
end
end
end
puts "This instance of GitLab Enterprise Edition is licensed to:"
$license.licensee.each do |key, value|
puts "#{key}: #{value}"
end
if $license.expired?
puts "The license expired on #{$license.expires_at}"
elsif $license.will_expire?
puts "The license will expire on #{$license.expires_at}"
else
puts "The license will never expire."
end

执行license.rb

1
ruby license.rb

生成以下文件

1
GitLabBV.gitlab-license  license.rb  license_key  license_key.pub
  • GitLabBV.gitlab-license : 页面导入的License文件
  • license_key.pub :覆盖原有的key

查看license_key.pub

1
2
3
4
5
6
7
8
9
10
root@gitlab:/home/jonty/gitlab/backups# cat license_key.pub 
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAtRXZpH9H6EXFEohYjLcZ
uAYoSVJfZjdpRnT/76JlNfHOOA6RbCBzzwN5FJkyCev/wXCe5P+jR1+jqp0DErrF
3/tYe3ZkUyDkD8c54AMpNqeXeevB+EEz4KqdsO9Q5260jlKxfTEVSQLZaItRfi0s
kq25nPRghOSX7kTL/riQA9dg2L0/of5ewgS164cTtgjeTKBCoOKN/vfw/GC0PDWn
Mf9OaR0C7NtOGesJBCWR47QjnPOjXHFip/yt0K8ytc42XGgzdJjFjPrUuZ1hbzJ7
Y8W9EvWu9+k9iupBK9XeeTORZDZYBvpHvAT2orEoLN1IUX7Bo12c2ddagfoSWp0v
swIDAQAB
-----END PUBLIC KEY-----

复制后进入容器内,替换密钥

1
2
3
docker exec -it gitlab /bin/bash
vi /opt/gitlab/embedded/service/gitlab-rails/.license_encryption_key.pub
exit

重启容器

1
docker compose restart

导入License文件,进入管理中心=> 通用=>添加许可证

导入GitLabBV.gitlab-license文件

image-20230627174050150

激活完成

image-20230627174223096