获取镜像

关于如何安装 Docker ,以及常用命令这里就不再做过多赘述

docker pull gitlab/gitlab-ce:latest

运行容器

在开始之前需要确定 80、443、22 端口是否被占用,如果前两个被占用了,可以考虑在现有服务器上进行反向代理。如果是 22号端口 被占用,那么建议修改系统的 SSHD 服务监听端口。这样当你使用 SSH 方式管理 Git 仓库时也比较方便,毕竟需要 SSH 到服务器的概率要比你提交代码的概率低的。

docker run --detach \
  --hostname gitlab.example.com \
  --env GITLAB_OMNIBUS_CONFIG="external_url 'https://gitlab.example.com/'; gitlab_rails['lfs_enabled'] = true;" \
  --publish 443:443 --publish 80:80 --publish 22:22 \
  --name gitlab \
  --restart always \
  --volume /srv/gitlab/config:/etc/gitlab \
  --volume /srv/gitlab/logs:/var/log/gitlab \
  --volume /srv/gitlab/data:/var/opt/gitlab \
  gitlab/gitlab-ce:latest

将上面的 gitlab.example.com 替换成你自己的域名,如果需要 HTTPS 的话 external_url 选项中的 URL 就写成 https://gitlab.example.com/,否则就用 http://gitlab.example.com/

上面的命令运行后如果你的域名解析没有问题的话,就可以打开浏览器正常访问了。

使用 HTTPS

自带 Let’s Encrypt

Gitlab 10.7 版本内置了 Let's Encrypt,你只需要配置 /etc/gitlab/config/gitlab.rb 如下几个参数就可以,自动启用 HTTPS 了。

letsencrypt['enable'] = true
external_url "https://gitlab.example.com" #如果你在运行容器时已经指定了,则无需再做配置
letsencrypt['contact_emails'] = ['user@example.com']  # 填写你的邮箱,此项非必填
# 自动续签的配置
letsencrypt['auto_renew'] = true
letsencrypt['auto_renew_hour'] = "12"
letsencrypt['auto_renew_minute'] = "30"
letsencrypt['auto_renew_day_of_month'] = "*/7"

使配置生效

docker exec -it gitlab /bin/bash # gitlab 替换成你运行容器时的 --name 的配置

gitlab-ctl renew-le-certs # 先生成证书

gitlab-ctl reconfigure # 使配置生效

如果先执行 gitlab-ctl reconfigure 命令,很有可能会报如下错误:

There was an error running gitlab-ctl reconfigure:
letsencrypt_certificate[gitlab.betterde.com] (letsencrypt::http_authorization line 3) had an error: RuntimeError: acme_certificate[staging] (/opt/gitlab/embedded/cookbooks/cache/cookbooks/letsencrypt/resources/certificate.rb line 20) had an error: RuntimeError: [gitlab.betterde.com] Validation failed for domain gitlab.betterde.com

如果一切正常,那么此时访问 https://gitlab.example.com 就能正常打开 Gitlab 的首页了。

I hope this is helpful, Happy hacking…