簡體   English   中英

Docker:無法在運行時克隆github私有rebo

[英]Docker: Unable to clone a github private rebo inside at run time

我創建了一個帶有.sh腳本作為條目文件的容器。 此外,dockerfile創建一個新用戶,其home為工作目錄。 .sh腳本本身位於新用戶的工作目錄中。

在運行時( docker run ),我可以看到容器執行.sh,因此構建成功。

我的問題是這個容器需要克隆一個私有的github repo。

在你關閉/投票關閉/標記為重復這個問題之前,讓我問你的幫助,因為我已經用Google搜索並閱讀了50多個關於這個問題的SO問題,但我沒有找到一個有效的例子。 我的問題是關於問題的解決方法以及如何實施

我的問題是git clone命令告訴我:

Cloning into 'tools'...
Host key verification failed.
fatal: Could not read from remote repository.

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

我認為我應該創建一個私鑰並將其添加到我的密鑰中添加到我的Github配置文件中,但我不能在每次運行時手動添加新的ssh密鑰。 對?

可能,我應該在構建時創建一個新密鑰並將其添加到我的github倉庫中。 圖像將始終是私有的,因此這方面沒有安全問題。 但是怎么做呢?

有沒有其他方法可以完成這項任務?

例如,我試圖在運行時復制我的工作私有rsa鍵:

docker run -it --rm my_image:git_cloning -v ~/.ssh/id_rsa:/realtebo/.ssh/id_rsa:ro

無論如何我得到了這個:

Cloning into 'tools'...
The authenticity of host 'github.com (192.30.253.113)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,192.30.253.113' (RSA) 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.

在構建時,我避免了執行github密鑰掃描的“添加密鑰問題”

RUN mkdir ~/.ssh \
    && echo >>  ~/.ssh/known_hosts \
    && ssh-keyscan github.com >> ~/.ssh/known_hosts 

但無論如何我在運行時得到了這個:

Cloning into 'tools'...
Warning: Permanently added the RSA host key for IP address '192.30.253.113' 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.

我終於解決了。

我將包含我的最終Dockerfile以及逐步說明

FROM <base_image>

在這種情況下,我從一個客戶形象開始,但在起源它是基於官方的ubuntu:16_04

RUN useradd -ms /bin/bash realtebo

作為第一個動作,我創建了新的非root用戶; 讀者應該記住,如果沒有對基本操作系統映像進行任何修改,容器將只有root用戶。

COPY id_rsa /home/realtebo/.ssh/
COPY id_rsa.pub /home/realtebo/.ssh/

我將公鑰和私鑰復制到Dockerfile文件夾中。 通過這些命令,我​​有效地為新用戶安裝了它們

請記住: COPY僅適用於Dockerfile的同一目錄(上下文)中的文件/目錄!

另外:必須將公鑰添加到您的Github SSH Keys錢包中

RUN chown realtebo:realtebo /home/realtebo \
    && chown realtebo:realtebo /home/realtebo/.ssh \
    && chown realtebo:realtebo /home/realtebo/.ssh/*

系統需要以新用戶身份訪問這些密鑰,因此我需要這些chown命令(僅使用1層)。

USER realtebo

從這一點開始,我繼續使用新用戶進行操作

RUN echo >>  ~/.ssh/known_hosts \
    && ssh-keyscan github.com >> ~/.ssh/known_hosts \
    && cd /home/realtebo \
    && git clone git@github.com:realtebo/my-private-tool.git tools 

第一行創建一個空的known_hosts文件(如果不存在),或者如果它已經存在則不附加任何內容。 當在clone時刻從github.com中檢索主機ip時,將從git使用此文件。 該主機名實際上是從其DNS系統綁定到多個IP。

第二行將所有已知的主機公鑰導入我們的known_hosts文件。

第3行將工作目錄更改為用戶的家。

第4行終於將我的工具真正克隆到tools子目錄中

我建議使用部署密鑰而不是用戶密鑰,這樣您就可以控制容器對存儲庫的訪問,而不會暴露您自己的密鑰。

我也做了類似的事情,像這樣共享ssh-agent套接字:

docker run \
--volume $SSH_AUTH_SOCK:/ssh-agent \
--env SSH_AUTH_SOCK=/ssh-agent

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM