DockerHubで公開されているGitLab CEのコンテナイメージを使えば、簡単にGitLabをローカルに立てることができます。
- GitLab CEをDockerで動かす
- GitLab CEでコンテナレジストリを動かす
はそれぞれ先人たちのブログ記事とか見つかる(ありがとうございます!)のですが、「Dockerで動かすGitLab CEにコンテナレジストリも追加した」がなかなか見当たらなかったので試してみました。
(レジストリ無しでデプロイしてdocker exec
でシェル起動して設定変更してgitlab-ctl reconfigure
でも試せた)
gitlab-ce.example.org
というドメイン名で動かす前提で、Gitリポジトリのポート番号は8443/TCP、コンテナレジストリは25000/TCPでhttpsさせてます。
(ウェルノウンじゃないポート使う時の設定例ということで見てもらえれば。)
※ 1/31 21:30更新
- GitLab起動時のレジストリ用ボリューム設定が抜けていたのを追加
- Dockerアクセス時のポート番号が間違っていたのを修正
関連
自己署名の証明書の作成
$ sudo mkdir /opt/gitlab-cert $ sudo openssl req -newkey rsa:4096 -nodes -sha256 -keyout /opt/gitlab-cert/gitlab-ce.example.org.key -x509 -days 3650 -out /opt/gitlab-cert/gitlab-ce.example.org.crt -subj '/CN=gitlab-ce.example.org/'
実行例
[zaki@registry ~]$ sudo openssl req -newkey rsa:4096 -nodes -sha256 -keyout /opt/gitlab-cert/gitlab-ce.example.org.key -x509 -days 3650 -out /opt/gitlab-cert/gitlab-ce.example.org.crt -subj '/ CN=gitlab-ce.example.org/' Generating a 4096 bit RSA private key ............................++ .................................................................................++ writing new private key to '/opt/gitlab-cert/gitlab-ce.example.org.key' ----- [zaki@registry ~]$ [zaki@registry ~]$ ls -l /opt/gitlab-cert/ 合計 8 -rw-r--r--. 1 root root 1822 1月 31 08:03 gitlab-ce.example.org.crt -rw-r--r--. 1 root root 3272 1月 31 08:03 gitlab-ce.example.org.key
GitLab起動
2020.01.31時点で12.7.4-ce.0
がlatestみたいなので、それを使います。
一覧
基本はdocker pull gitlab/gitlab-ce:12.7.4-ce.0
sudo docker run --detach \ --hostname gitlab-ce.example.org \ --env GITLAB_OMNIBUS_CONFIG="external_url 'https://gitlab-ce.example.org:8443'; nginx['listen_port']=8443; gitlab_rails['registry_enabled']=true; registry_external_url 'https://gitlab-ce.example.org:25000'" \ --publish 8443:8443 --publish 25000:25000 \ --name gitlab-registry \ --restart always \ --volume /opt/gitlab-reg/config:/etc/gitlab:Z \ --volume /opt/gitlab-reg/logs:/var/log/gitlab:Z \ --volume /opt/gitlab-reg/data:/var/opt/gitlab:Z \ --volume /opt/gitlab-reg/registry:/var/opt/gitlab/gitlab-rails/shared/registry:Z \ --volume /opt/gitlab-cert:/etc/gitlab/ssl \ gitlab/gitlab-ce:12.7.4-ce.0
※ 1/31 21:30追記: レジストリ用のボリューム設定が抜けていたので追加
おしまい。
しばらくまってブラウザからアクセスすればOK
起動したらまずroot
ユーザのパスワード設定。
それができたら通常ユーザのアカウント作成すればOK
適当にプロジェクトを作るとPackagesメニューの中にContainer Registryというのが見えていればOK
別ホストのDockerクライアントからこのレジストリを使う場合の証明書設定
そのままdocker login
しても証明書設定されてないのでエラーになります。
※ 1/31 21:30: ポート番号が8443(Gitリポジトリ用)になっていたのを25000(コンテナレジストリ用)に修正
[zaki@manager-dev ~]$ sudo docker login gitlab-ce.example.org:25000 Username: zaki Password: Error response from daemon: Get https://gitlab-ce.example.org:25000/v1/users/: x509: certificate signed by unknown authority
設定は/etc/docker/certs.d
以下にアクセスするFQCN:ポート番号
のディレクトリ作って、最初に作成したcrtファイルをそこに置けばOK (ファイル名はなんでもよさげ)
$ sudo mkdir -p /etc/docker/certs.d/gitlab-ce.example.org:25000 $ scp gitlab-ce.example.org:/opt/gitlab-cert/gitlab-ce.example.org.crt . $ sudo cp gitlab-ce.example.org.crt /etc/docker/certs.d/gitlab-ce.example.org\:25000/ $ sudo systemctl restart docker
Dockerを再起動すれば
[zaki@manager-dev ~]$ sudo docker login gitlab-ce.example.org:25000 Username: zaki Password: Login Succeeded
はい
2020.01.31 21:30追記
今朝のブログ作成中、出社前ということで急いでたので(←言い訳)気付かなかったというか手順間違えてたんだけど、docker login
、ログイン成功してたので気付かなかったんだけど、コンテナレジストリ用に設定したはずの25000/TCPでなくて、Gitリポジトリ用の8443/TCPに接続してました。
# 誤り $ sudo docker login gitlab-ce.example.org:8443
# 正解 $ sudo docker login gitlab-ce.example.org:25000
ちなみにこれ、Gitリポジトリ用の8443/TCPへのdocker login
は(多分認証系のHTTPレベルしか見てないのかも)成功するけど、push
とかその他のDocker APIはエラー(普通にGitLabのHTTP 404 レスポンスのページ)になる。
$ sudo docker push gitlab-ce.example.org:8443/zaki/example/httpd:latest The push refers to a repository [gitlab-ce.example.org:8443/zaki/example/httpd] 61286bfc1560: Preparing 6f0982f743c3: Preparing 8032b0dc1204: Preparing 525297c1b6d2: Preparing 556c5fb0d91b: Preparing Error: Status 404 trying to push repository zaki/example/httpd: "<!DOCTYPE html>\n<html>\n<head>\n ......
こんな感じ
同時刻
レジストリデータのボリューム設定が抜けていたので、コンテナを作り直すとデータ消えちゃいます…すみません。
--volume /opt/gitlab-reg/registry:/var/opt/gitlab/gitlab-rails/shared/registry:Z
設定変えて作り直す場合
# 止めて $ sudo docker stop gitlab-registry gitlab-registry # 消して $ sudo docker rm gitlab-registry gitlab-registry # 作り直す $ sudo docker run --detach \ --hostname gitlab-ce.example.org \ --env GITLAB_OMNIBUS_CONFIG="external_url 'https://gitlab-ce.example.org:8443'; nginx['listen_port']=8443; gitlab_rails['registry_enabled']=true; registry_external_url 'https://gitlab-ce.example.org:25000'" \ --publish 8443:8443 --publish 25000:25000 \ --name gitlab-registry \ --restart always \ --volume /opt/gitlab-reg/config:/etc/gitlab:Z \ --volume /opt/gitlab-reg/logs:/var/log/gitlab:Z \ --volume /opt/gitlab-reg/data:/var/opt/gitlab:Z \ --volume /opt/gitlab-reg/registry:/var/opt/gitlab/gitlab-rails/shared/registry:Z \ --volume /opt/gitlab-cert:/etc/gitlab/ssl \ gitlab/gitlab-ce:12.7.4-ce.0
のが基本だけど、--name
で指定する名前を変更すれば、消さずにstop
だけでも大丈夫。ただ、中でDB動いてるので、ボリューム領域を共有するのはあまり良くないので同時起動はしない方が吉。
SSHでcloneする設定が抜けてるので使う場合は追加。
参考
- まずは公式 GitLab Docker images | GitLab
- SSL設定 SSL Configuration | GitLab
- レジストリ設定 GitLab Container Registry administration | GitLab