Docker イメージビルドで SSH を使用して Git クローンする

Docker イメージビルドで SSH を使用して Git クローンする

Docker イメージを構築しており、Bitbucket からリポジトリをクローンしたいと考えています。

`debian' コンテナを作成し、ステップごとに実行すると、すべて正常に動作します。しかし、イメージを作成しようとすると、動作しません。

Bitbucket設定にキーを追加しました。

これが私のDockerfile

FROM debian:stretch

RUN apt-get update && apt-get -y upgrade && apt-get -y install nginx curl software-properties-common gnupg git
RUN curl -sL https://deb.nodesource.com/setup_6.x | bash -
RUN apt-get install -y nodejs

RUN mkdir /backend

RUN npm install pm2 ts-node -g

WORKDIR /backend
RUN mkdir /root/.ssh
RUN echo -e "-----BEGIN RSA PRIVATE KEY-----\n(...)-----END RSA PRIVATE KEY-----" >> /root/.ssh/id_rsa
RUN chmod 400 /root/.ssh/id_rsa
RUN ssh-keyscan bitbucket.org >> /root/.ssh/known_hosts
RUN git clone [email protected]:xxx/xxx.git

エラーは次のとおりです:

Cloning into 'xxx'...
Warning: Permanently added the RSA host key for IP address '104.192.143.3' to the list of known hosts.
Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

このイメージを適切に動作させるにはどうすればよいでしょうか?

答え1

正しい公開鍵が Bitbucket にあることが確実な場合、答えは (私の経験では) ほとんどの場合、.ssh フォルダーとその中にあるファイルの権限です。上記では、フォルダーとその中に秘密鍵を作成するだけで、権限を更新していないことがわかります。

期待される権限

.ssh は次のようになります:

drwx------  2 user user 4096 Feb  6 11:18 .ssh

秘密鍵:

-rw-------  1 user user 1675 Feb  6 11:18 id_rsa

最後に、ホーム ディレクトリは少なくともグループまたは他のユーザーによって書き込み可能にならないようにする必要があります。一般的には、次のようになります。

drwx------ 84 user user 16384 Feb 16 18:23 user

すべてを一緒に入れて:

chmod go-w /root
chmod 700 /root/.ssh
chmod 600 /root/.ssh/id_rsa

関連情報