Other

Docker 快速部署Gitlab

Docker 快速部署 Gitlab CE

1. 运行实例

# -e GITLAB_OMNIBUS_CONFIG="external_url 'https://gitlab.xxx.com'"
export GITLAB_HOME=/mnt/disk1/gitlab
docker run -d \
--hostname gitlab.xxx.com \
--name gitlab \
--restart always \
--ulimit nofile=8192 \
--ulimit memlock=-1 \
--memory=3G \
--memory-swappiness=50 \
--cpuset-cpus='1-2' \
-e EXTERNAL_URL=https://gitlab.xxx.com \
-p 5443:443 \
-p 5080:80 \
-p 5022:22 \
-v $GITLAB_HOME/config:/etc/gitlab \
-v $GITLAB_HOME/logs:/var/log/gitlab \
-v $GITLAB_HOME/data:/var/opt/gitlab gitlab/gitlab-ce:12.10.7-ce.0

2. 配置邮箱

docker exec -it gitlab bash
vim /etc/gitlab/gitlab.rb
  • 以163邮箱为例
gitlab_rails['smtp_enable'] = true
gitlab_rails['smtp_address'] = "smtp.163.com"
gitlab_rails['smtp_port'] = 25
gitlab_rails['smtp_user_name'] = "xxxx@163.com"
gitlab_rails['smtp_password'] = "xxxxpassword"
gitlab_rails['smtp_domain'] = "163.com"
gitlab_rails['smtp_authentication'] = "login"
gitlab_rails['smtp_enable_starttls_auto'] = false
gitlab_rails['smtp_openssl_verify_mode'] = "peer"
gitlab_rails['gitlab_email_from'] = "xxxx@163.com"
user["git_user_email"] = "xxxx@163.com"

注意以上的xxxx@163.com代表用户名,即邮箱地址,而xxxxpassword不是邮箱的登陆密码而是网易邮箱的客户端授权密码, 再网易邮箱web页面的设置-POP3/SMTP/IMAP-客户端授权密码查看。

2.2 配置外部访问URL(可选,若启动时未设置 EXTERNAL_URL)

这个必须配置,否则默认以容器的主机名作为URL,上面 5080 映射到 gitlab 容器的8080(因为gitlab默认使用80对外服务),创建完成后必需进入 gitlab 容器修改 vim /etc/gitlab/gitlab.rb 内容为:

docker exec -it gitlab bash
vim /etc/gitlab/gitlab.rb

注: 每次重启docker或gitlab的容器,都会将 /etc/gitlab/gitlab.rb下的external_url 地址的端口(如:上面是8080)刷新到nginx配置文件: /var/opt/gitlab/nginx/conf/gitlab-http.conf,这样就需注意必须与 docker run... 创建容器时映射的端口一致,否则下次重启nginx端口配置就会被覆盖,访问不了gitlab。

后来发现external_url只能配置ip或者域名,不能有端口,否则不能启动。
于是只能把端口设置为80->80,然后external_url设置为:

external_url "http://10.103.240.36"

3. 重启gitlab

docker restart gitlab

参考文献:https://segmentfault.com/a/1190000000345686

4. FAQ

4.1. HTTP 502: Whoops, GitLab is taking too much time to respond.

gitlab-ctl stop

# 取消注释并修改端口
vi /etc/gitlab/gitlab.rb

unicorn['port'] = 8801

# 重新生成配置
gitlab-ctl reconfigure
gitlab-ctl restart

# check whether unicorn has started properly.
lsof -i:8081

有时候修改配置后要不断刷新才能生效

4.2. git push 报错 error: RPC failed; HTTP 413 curl 22 The requested URL returned error: 413 Request Entity Too Large

4.2.1 解决方案1(配置永久生效):

vim /etc/gitlab/gitlab.rb
# 找到 client_max_body_size 配置调大到500m
nginx['client_max_body_size'] = '500m'

# 然后重新生成配置&重启
gitlab-ctl reconfigure
gitlab-ctl restart

# 验证是否更新配置
vim /var/opt/gitlab/nginx/conf/gitlab-http.conf
# 找到已更新的配置
  ## Increase this if you want to upload large attachments
  ## Or if you want to accept large git objects over http
  client_max_body_size 500m;

# 设置git客户端buffer
git config --global http.postBuffer 2048576000

# 重新推送
git push

4.2.2 解决方案2:

由于 gitlab中nginx有限制,可改用 ssh 地址提交.

git remote -v
git remote set-url origin git@xxx.domain.com/project1.git
# 生成 ssh 密钥
ssh-keygen -t ed25519 -C "email@example.com" #推荐
# 或者
ssh-keygen -o -t rsa -b 4096 -C "email@example.com"

https://docs.gitlab.com/ee/ssh/#generate-an-ssh-key-pair

4.3. 点击项目Clone按钮,地址却是localhost(或主机名),如何修改为域名地址?

vim /opt/gitlab/embedded/service/gitlab-rails/config/gitlab.yml
修改节点(注不包含http前缀):production.gitlab.host=git.xxx.com

5.4 如何通过 gitlab psql 修改 project.updated_at

  • 进入 gitlab 容器并查看 psql 信息
# 进入容器
docker exec -it gitlab bash

# 查看 gitlab postgresql 连接信息
cat /var/opt/gitlab/gitlab-rails/etc/database.yml
# This file is managed by gitlab-ctl. Manual changes will be
# erased! To change the contents below, edit /etc/gitlab/gitlab.rb
# and run `sudo gitlab-ctl reconfigure`.
production:
  adapter: postgresql
  encoding: unicode
  collation: 
  database: gitlabhq_production
  pool: 1
  username: "gitlab"
  password: 
  host: "/var/opt/gitlab/postgresql"
  port: 5432
  socket: 
  sslmode: 
  sslcompression: 0
  sslrootcert: 
  sslca: 
  load_balancing: {"hosts":[]}
  prepared_statements: false
  statement_limit: 1000
  fdw: 
  variables:
    statement_timeout: 60000
  • 查看用户名映射
cat /etc/passwd | grep gitlab-psql
gitlab-psql:x:996:996::/var/opt/gitlab/postgresql:/bin/sh

id gitlab-psql
uid=996(gitlab-psql) gid=996(gitlab-psql) groups=996(gitlab-psql)
  • 进入 psql 命令行
# 切换映射用户
su - gitlab-psql

# 进入 psql
psql -h /var/opt/gitlab/postgresql -d gitlabhq_production
psql (11.7)
Type "help" for help.

# 查看 psql 版本
gitlabhq_production=# select version();
# 查看数据库实例
gitlabhq_production=# \l
# 查看schema
gitlabhq_production=# \dn
# 查看用户和角色
gitlabhq_production=# \du
# 查看表空间
gitlabhq_production=# \db
# 查看权限
gitlabhq_production=# \dp
# 查看扩展信息
gitlabhq_production=# \dx
# 查看用户详细信息
gitlabhq_production=# select * from pg_user;
# 查询表信息
gitlabhq_production=# \dt
# 查看单个表结构
gitlabhq_production=# \d projects
# 查看视图
gitlabhq_production=# \dv
# 修改 project updated_at(group 列表页面展示的 lastUpdate 时间,非 git commit 时间)
gitlabhq_production=# select updated_at from projects where id=101;
gitlabhq_production=# update projects set updated_at='2018-06-29 02:33:34.344719' where id=101;

留言

您的电子邮箱地址不会被公开。