使用 docker 搭建 gitlab

此文档使用 docker 搭建 gitlab-ce:15.11.4版本。

如果不是使用gitlab镜像自带的PostgreSQL数据库,那么就需要手动创建数据库表:

create user gitlab;
alter user gitlab password '123456';
create database gitlab;
alter database gitlab owner to gitlab;
\c gitlab
create extension "pg_trgm";

编辑docker-compose.yml 文件,内容如下:

version: '3'
services:
  gitlab:
    container_name: gitlab
    hostname: gitlab
    image: gitlab/gitlab-ce:15.11.4-ce.0
    ports:
      - "8081:8081"
      - "4422:22"
    restart:
      always
    #userns_mode:
    #  "host"
    volumes:
      - $PWD/config:/etc/gitlab
      - $PWD/logs:/var/log/gitlab
      - $PWD/data:/var/opt/gitlab
      - /etc/localtime:/etc/localtime:ro

启动docker-compose

docker compose up -d

编辑config/gitlab.rb文件,在最后追加配置:

external_url 'http://192.168.4.9:8081'
gitlab_rails['time_zone'] = 'Asia/Shanghai'
gitlab_rails['gitlab_email_from'] = "123456@qq.com"
gitlab_rails['gitlab_email_display_name'] = 'gitlab server'
gitlab_rails['gitlab_shell_ssh_port'] = 4422
postgresql['enable'] = true
gitlab_rails['db_adapter'] = "postgresql"
gitlab_rails['db_encoding'] = "utf8"
gitlab_rails['db_host'] = "192.168.1.240"
gitlab_rails['db_port'] = 5432
gitlab_rails['db_username'] = "gitlab"
gitlab_rails['db_password'] = "123456"
gitlab_rails['db_database'] = "gitlab"
gitlab_rails['smtp_enable'] = true
gitlab_rails['smtp_address'] = "smtp.qq.com"
gitlab_rails['smtp_port'] = 465
gitlab_rails['smtp_user_name'] = "123456@qq.com"
gitlab_rails['smtp_password'] = "suvywq"
gitlab_rails['smtp_domain'] = "qq.com.com"
gitlab_rails['smtp_authentication'] = "login"
gitlab_rails['smtp_enable_starttls_auto'] = false
gitlab_rails['smtp_tls'] = true
gitlab_rails['smtp_openssl_verify_mode'] = 'none'
puma['worker_processes'] = 2
prometheus['enable'] = false

更改完成后需要在容器内重新加载配置文件:

gitlab-ctl reconfigure

如果reconfigure成功,最后会提示gitlab Reconfigured!

之后可以通过浏览器访问 GitLab,账号 root,默认密码在/etc/gitlab/initial_root_password

在/etc/crontab里设置gitlab备份:

# gitlab backup
00 06 * * * root /usr/bin/docker exec gitlab gitlab-rake gitlab:backup:create >> /data/docker_data/gitlab/data/backups/backup.log
00 06 * * * root /usr/bin/find /data/docker_data/gitlab/data/backups -name '*.tar' -mtime +30 | /usr/bin/xargs rm -f

注意:当使用git 连接gitlab的非22端口的SSH服务时如发现需要输入密码,私钥无法使用,或者出现以下提示:

stderr: Warning: Permanently added '192.168.4.9' (ECDSA) to the list of known hosts.
Permission denied, please try again.
Permission denied, please try again.
git@192.168.4.9: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).
fatal: Could not read from remote repository.

就要使用新的方式,在git地址前面加上ssh://,比如:

git clone ssh://git@hostname:port/.../xxx.git