![在 docker 映像建置中使用 ssh 進行 Git 克隆](https://rvso.com/image/1552393/%E5%9C%A8%20docker%20%E6%98%A0%E5%83%8F%E5%BB%BA%E7%BD%AE%E4%B8%AD%E4%BD%BF%E7%94%A8%20ssh%20%E9%80%B2%E8%A1%8C%20Git%20%E5%85%8B%E9%9A%86.png)
我正在建立一個 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