LoginSignup
0
1

More than 1 year has passed since last update.

docker-compose build時のgithub ssh接続でハマった(exit code 1)

Posted at

意外に情報を探すのに苦労したので、書き残します。

やりたいこと

  • proxy環境下のlinuxサーバにDockerコンテナを構築し、そのコンテナ内にgitlabとgithubのディレクトリを用意し、それぞれにおいてgitlabアカウントとgithubアカウントを使い分けたい。
  • コンテナを立ち上げてから毎回色々コマンドを打つのは面倒なので、できるだけdocker-compose build時にやってしまいたい。

なお、gitlabに関してはすでに設定ができていましたので、githubの設定をする際のハマりについて書きます。

構築

ネットにすでに色々な情報が散らばっているので、それらを踏まえ、次のようにgitlabおよびgithubへのssh接続を記述。
基本的にはgitlabアカウントを、githubディレクトリ以下ではgithubアカウントを使うようにします。

Dockerfile
FROM jupyter/datascience-notebook

(略)

RUN mkdir -p /root/.ssh/ && apt-get install -y netcat
COPY ./settings/config /root/.ssh/config
COPY ./settings/.gitconfig_main /home/jovyan/.gitconfig
COPY ./settings/.gitconfig_sub /home/jovyan/.gitconfig_sub
COPY ./secrets/gitlab_rsakey /root/.ssh/gitlab_rsakey
COPY ./secrets/github_rsakey /root/.ssh/github_rsakey

RUN chmod 600 /root/.ssh/config && chmod 600 /root/.ssh/gitlab_rsakey && chmod 600 /root/.ssh/github_rsakey && \
    eval "$(ssh-agent -s)" && ssh-add /root/.ssh/gitlab_rsakey && ssh-add /root/.ssh/github_rsakey && \
    ssh -y -T git@gitlab.com && ssh -y -T git@github.com
.settings/config
Host gitlab.com
    HostName gitlab.com
    User taro-gitlab
    IdentityFile /root/.ssh/gitlab_rsakey
    StrictHostKeyChecking no
    AddKeysToAgent yes

Host github.com
    HostName ssh.github.com
    User taro-github
    IdentityFile /root/.ssh/github_rsakey
    StrictHostKeyChecking no
    TCPKeepAlive yes
    IdentitiesOnly yes
    Port 443
    ProxyCommand nc -X connect -x proxy_ip:proxy_port %h %p
    AddKeysToAgent yes
.settings/.gitconfig_main
[user]
        name = taro-gitlab
        email = taro@for-gitlab.com

[includeIf "gitdir:~/github/"]
        path = ~/.gitconfig_sub
.settings/.gitconfig_sub
[user]
        name = taro-github
        email = taro@for-github.com

本題ではないので詳細は省きますが、少しだけ補足すると、

  • githubへの接続の際、linuxサーバ環境の設定上22ポートが使えず、https用に開放されている443ポートを使ってssh
  • その場合、configに記載したHostNameはgithub.comではなく、ssh.github.comに
  • ProxyCommandとPortの記述も反映

エラー発生

これをdocker-compose buildした結果は次のとおり。

docker-compose buildの実行結果
Step 42/61 : RUN chmod 600 /root/.ssh/config && chmod 600 /root/.ssh/gitlab_rsakey && chmod 600 /root/.ssh/github_rsakey &&    eval "$(ssh-agent -s)" && ssh-add /root/.ssh/gitlab_rsakey && ssh-add /root/.ssh/github_rsakey &&   ssh -y -T git@gitlab.com && ssh -y -T git@github.com
 ---> Running in b914e3293de5
Agent pid 12
Identity added: /root/.ssh/gitlab_rsakey (taro@for-gitlab.com)
Identity added: /root/.ssh/github_rsakey (taro@for-github.com)
Welcome to GitLab, @taro-gitlab!
Hi taro-github! You've successfully authenticated, but GitHub does not provide shell access.
ERROR: Service 'jupyter-git' failed to build: The command '/bin/bash -o pipefail -c chmod 600 /root/.ssh/config && chmod 600 /root/.ssh/gitlab_rsakey && chmod 600 /root/.ssh/github_rsakey &&    eval "$(ssh-agent -s)" && ssh-add /root/.ssh/gitlab_rsakey && ssh-add /root/.ssh/github_rsakey &&   ssh -y -T git@gitlab.com && ssh -y -T git@github.com' returned a non-zero code: 1

!?
最後のsshコマンドが通った後に、なぜか異常終了…。
正常にコマンドを終えているように見えるのになぜ…。

エラー調査

sshコマンドのオプションを-Tから-vTに変えて再度実行し、詳細を見ます。見るべきはgithubにログイン成功した後ですよね。

(前略)
Hi taro-github! You've successfully authenticated, but GitHub does not provide shell access.
debug1: channel 0: free: client-session, nchannels 1
debug1: fd 0 clearing O_NONBLOCK
debug1: fd 2 clearing O_NONBLOCK
Transferred: sent 2784, received 2292 bytes, in 0.3 seconds
Bytes per second: sent 9421.1, received 7756.2
debug1: Exit status 1

うーん、よく分からない。
仕方なくしばらくwebを漁っていたところ、Stack Overflowで次の情報を発見!
Check ssh with Github.com before running a script

要するに、ログインした時のメッセージ "but GitHub does not provide shell access."に注目すべきで、shell accessができないので、エラーになっているということのよう。
ご親切にシェルスクリプトまで記載して回答してくれているので、これを使います。
ただし、docker-compose build時にこれを組み込むには、少しだけ工夫が必要です。

解決

最初に記載したDockerfileを次のように直します。(ssh_github.shに関して追加)

Dockerfile
FROM jupyter/datascience-notebook

(略)

RUN mkdir -p /root/.ssh/ && apt-get install -y netcat
COPY ./settings/config /root/.ssh/config
COPY ./settings/.gitconfig_main /home/jovyan/.gitconfig
COPY ./settings/.gitconfig_sub /home/jovyan/.gitconfig_sub
COPY ./secrets/gitlab_rsakey /root/.ssh/gitlab_rsakey
COPY ./secrets/github_rsakey /root/.ssh/github_rsakey
COPY ./settings/ssh_github.sh /root/ssh_github.sh

RUN chmod 600 /root/.ssh/config && chmod 600 /root/.ssh/gitlab_rsakey && chmod 600 /root/.ssh/github_rsakey && \
    eval "$(ssh-agent -s)" && ssh-add /root/.ssh/gitlab_rsakey && ssh-add /root/.ssh/github_rsakey && \
    ssh -y -T git@gitlab.com && ssh -y -T git@github.com
RUN bash /root/ssh_github.sh
.settings/ssh_github.sh
#!/bin/bash

function github-authenticated() {
  # Attempt to ssh to GitHub
  ssh -T git@github.com &>/dev/null
  RET=$?
  if [ $RET == 1 ]; then
    # user is authenticated, but fails to open a shell with GitHub
    echo "You've successfully authenticated, but GitHub does not provide shell access."
    return 0
  elif [ $RET == 255 ]; then
    # user is not authenticated
    return 1
  else
    echo "unknown exit code in attempt to ssh into git@github.com"
  fi
  return 2
}

github-authenticated

これで解決しました!

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1