zaki work log

作業ログやら生活ログやらなんやら

GitLab CE on Docker Compose (HTTPS + Container Registry + Runner)でSSH設定してなかったので追加

GitLab CE on Docker(Docker Compose)のSSH設定について。
SSHといっても、GitLabサーバーのメンテナンスに使うのでなく、GitリポジトリSSHアクセスに使用するためのもの。(シェルログインには使用しない)

ベースはこちら

zaki-hmkc.hatenablog.com

docs.gitlab.com

GitLab on DockerのSSH接続を有効化

GitLab自体のSSHはデフォルトで有効になってはいるんだけど、Dockerで動かしていると外部からのSSH接続は一工夫が必要なので設定する。

docker-compose.ymlの変更点はこちら

github.com

GitLabコンテナ内のSSHサーバーは22/TCPで動作しているけど、ホストOSで既にSSHが動作している場合は同じポート番号でpublishはできないため、ホストOSでは(ここでは)25022/TCPでListenして、それをGitLabコンテナの22/TCPフォワードする設定にする。

Docker Composeとしては、ports設定に "25022:22" を追加すればOK

ただそれだけだと、GitLabのwebの画面上で「Clone」ボタン押下したときの、コピー用テキストに25022/TCPの情報が含まれないので、gitlab_rails['gitlab_shell_ssh_port'] = 25022という設定を追加。

f:id:zaki-hmkc:20201212110513p:plain

publish設定の変更を伴うので、実行中のコンテナは削除して作り直す。

[zaki@registry gitlab-compose]$ sudo docker-compose down
Stopping gitlab-compose_gitlab-ce.example.org_1 ... done
Stopping gitlab-compose_gitlab-runner_1         ... done
Removing gitlab-compose_gitlab-ce.example.org_1 ... done
Removing gitlab-compose_gitlab-runner_1         ... done
Removing network gitlab-compose_gitlab-network
[zaki@registry gitlab-compose]$ 
[zaki@registry gitlab-compose]$ 
[zaki@registry gitlab-compose]$ sudo docker-compose up -d
Creating network "gitlab-compose_gitlab-network" with the default driver
Creating gitlab-compose_gitlab-ce.example.org_1 ... done
Creating gitlab-compose_gitlab-runner_1         ... done

これでホストOSは25022/TCPでListenしつつ、GitLabコンテナのSSHへ接続を転送する構成になる。

[zaki@registry gitlab-compose]$ sudo ss -anptl | grep 25022
LISTEN     0      128       [::]:25022                 [::]:*                   users:(("docker-proxy",pid=28264,fd=4))

StatusがhealthyになっていればOK

[zaki@registry gitlab-compose]$ sudo docker-compose ps
              Name                            Command                  State                    Ports              
-------------------------------------------------------------------------------------------------------------------
gitlab-compose_gitlab-             /assets/wrapper                  Up (healthy)   0.0.0.0:25022->22/tcp,          
ce.example.org_1                                                                   0.0.0.0:25000->25000/tcp,       
                                                                                   443/tcp, 80/tcp,                
                                                                                   0.0.0.0:8443->8443/tcp          
gitlab-compose_gitlab-runner_1     /usr/bin/dumb-init /entryp ...   Up       

webアクセスして、cloneボタンで設定したポート番号が付いていればOK

f:id:zaki-hmkc:20201212105004p:plain

公開鍵登録

ここからはユーザー側の通常操作

f:id:zaki-hmkc:20201212102945p:plain

ここに公開鍵(~/.ssh/***.pubcatか何かで開いてコピペ)を張り付けて「Add key」押下する。

cloneおためし

デフォルトファイル名以外の秘密鍵の場合は認識してくれないので、~/.ssh/configを設定しておく。
clone用テキストのホスト名と、それに対応する秘密鍵を設定すればOK

Host gitlab-ce.example.org
    IdentityFile ~/.ssh/id_rsa_4096

あとはclone用テキストを使ってclone,pushを確認できるはず。

[zaki@cloud-dev tmp]$ git clone ssh://git@gitlab-ce.example.org:25022/zaki/zzz.git
Cloning into 'zzz'...
warning: You appear to have cloned an empty repository.
[zaki@cloud-dev tmp]$ cd zzz/
[zaki@cloud-dev zzz (master)]$ ls
[zaki@cloud-dev zzz (master)]$ touch README.md
dd README"[zaki@cloud-dev zzz (master)]$ git add README.md
[zaki@cloud-dev zzz (master)]$ git commit -m "add README"
[master (root-commit) 3441ee7] add README
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 README.md
[zaki@cloud-dev zzz (master)]$ git push -u origin master
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 223 bytes | 223.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To ssh://gitlab-ce.example.org:25022/zaki/zzz.git
 * [new branch]      master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.

docker-compose.yml

全体のdocker-compose.ymlとしてはこんな感じ

---
version: '3'
services:
  gitlab-ce.example.org:
    image: gitlab/gitlab-ce:13.2.3-ce.0
    hostname: gitlab-ce.example.org
    restart: always
    ports:
      - "8443:8443"
      - "25000:25000"
      - "25022:22"
    environment:
      GITLAB_OMNIBUS_CONFIG: "external_url 'https://gitlab-ce.example.org:8443'; nginx['listen_port']=8443; gitlab_rails['gitlab_shell_ssh_port'] = 25022; gitlab_rails['registry_enabled']=true; registry_external_url 'https://gitlab-ce.example.org:25000'"
    volumes:
      - /opt/gitlab-reg/config:/etc/gitlab:Z
      - /opt/gitlab-reg/logs:/var/log/gitlab:Z
      - /opt/gitlab-reg/data:/var/opt/gitlab:Z
      - /opt/gitlab-reg/registry:/var/opt/gitlab/gitlab-rails/shared/registry:Z
      - /opt/gitlab-cert:/etc/gitlab/ssl
    networks:
      - gitlab-network
  gitlab-runner:
    image: gitlab/gitlab-runner:v13.2.2
    restart: always
    volumes:
      - /opt/gitlab-runner/config:/etc/gitlab-runner:Z
      - /var/run/docker.sock:/var/run/docker.sock
      - /opt/gitlab-cert:/etc/gitlab-runner/certs
    networks:
      - gitlab-network

networks:
  gitlab-network: