我从周围的各种工作dockerfile中复制了这段代码,这里是我的:

FROM ubuntu

MAINTAINER Luke Crooks "luke@pumalo.org"

# Update aptitude with new repo
RUN apt-get update

# Install software 
RUN apt-get install -y git python-virtualenv

# Make ssh dir
RUN mkdir /root/.ssh/

# Copy over private key, and set permissions
ADD id_rsa /root/.ssh/id_rsa
RUN chmod 700 /root/.ssh/id_rsa
RUN chown -R root:root /root/.ssh

# Create known_hosts
RUN touch /root/.ssh/known_hosts

# Remove host checking
RUN echo "Host bitbucket.org\n\tStrictHostKeyChecking no\n" >> /root/.ssh/config

# Clone the conf files into the docker container
RUN git clone git@bitbucket.org:Pumalo/docker-conf.git /home/docker-conf

这就给出了误差

Step 10 : RUN git clone git@bitbucket.org:Pumalo/docker-conf.git /home/docker-conf
 ---> Running in 0d244d812a54
Cloning into '/home/docker-conf'...
Warning: Permanently added 'bitbucket.org,131.103.20.167' (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.
2014/04/30 16:07:28 The command [/bin/sh -c git clone git@bitbucket.org:Pumalo/docker-conf.git /home/docker-conf] returned a non-zero code: 128

这是我第一次使用dockerfiles,但从我所读到的(以及从工作配置中获得的)我不明白为什么这行不通。

我的id_rsa与我的dockerfile在同一个文件夹中,是我的本地密钥的副本,可以克隆这个repo没有问题。

编辑:

在我的dockerfile我可以添加:

RUN cat /root/.ssh/id_rsa

它会打印出正确的密钥,所以我知道它被正确复制了。

我也试着按照诺亚的建议去做:

RUN echo "Host bitbucket.org\n\tIdentityFile /root/.ssh/id_rsa\n\tStrictHostKeyChecking no" >> /etc/ssh/ssh_config

不幸的是,这也行不通。


当前回答

您应该为Docker映像创建新的SSH密钥集,因为您可能不想在其中嵌入您自己的私钥。要使其工作,您必须将该密钥添加到git存储库中的部署密钥中。以下是完整的食谱:

使用ssh-keygen -q -t rsa -N " -f repo-key生成ssh密钥,这将为您提供repo-key和repo-key。酒吧的文件。 添加repo-key。发布到存储库部署密钥。 在GitHub上,进入[您的存储库]->设置->部署密钥 在Dockerfile中添加如下内容: ADD repo-key / 运行\ Chmod 600 /repo-key && \ echo "IdentityFile /repo-key" >> /etc/ssh/ssh_config && \ echo -e "StrictHostKeyChecking no" >> /etc/ssh/ssh_config && \ .使用实例 //你的git克隆命令在这里…

注意,上面关闭了StrictHostKeyChecking,所以您不需要.ssh/known_hosts。尽管我可能更喜欢上面答案中的ssh-keyscan解决方案。

其他回答

您应该为Docker映像创建新的SSH密钥集,因为您可能不想在其中嵌入您自己的私钥。要使其工作,您必须将该密钥添加到git存储库中的部署密钥中。以下是完整的食谱:

使用ssh-keygen -q -t rsa -N " -f repo-key生成ssh密钥,这将为您提供repo-key和repo-key。酒吧的文件。 添加repo-key。发布到存储库部署密钥。 在GitHub上,进入[您的存储库]->设置->部署密钥 在Dockerfile中添加如下内容: ADD repo-key / 运行\ Chmod 600 /repo-key && \ echo "IdentityFile /repo-key" >> /etc/ssh/ssh_config && \ echo -e "StrictHostKeyChecking no" >> /etc/ssh/ssh_config && \ .使用实例 //你的git克隆命令在这里…

注意,上面关闭了StrictHostKeyChecking,所以您不需要.ssh/known_hosts。尽管我可能更喜欢上面答案中的ssh-keyscan解决方案。

我的密钥是密码保护,这是导致问题的原因,一个工作文件现在列出如下(为未来的谷歌人提供帮助)

FROM ubuntu

MAINTAINER Luke Crooks "luke@pumalo.org"

# Update aptitude with new repo
RUN apt-get update

# Install software 
RUN apt-get install -y git
# Make ssh dir
RUN mkdir /root/.ssh/

# Copy over private key, and set permissions
# Warning! Anyone who gets their hands on this image will be able
# to retrieve this private key file from the corresponding image layer
ADD id_rsa /root/.ssh/id_rsa

# Create known_hosts
RUN touch /root/.ssh/known_hosts
# Add bitbuckets key
RUN ssh-keyscan bitbucket.org >> /root/.ssh/known_hosts

# Clone the conf files into the docker container
RUN git clone git@bitbucket.org:User/repo.git

附注:这个解决方案快速简单;但是以降低安全性为代价(参见@jrh的评论)。


创建访问令牌:https://github.com/settings/tokens

将它作为参数传递给docker (附注:如果你正在使用CapRover,在应用程序配置下设置它)

在Dockerfile中:

ARG GITHUB_TOKEN=${GITHUB_TOKEN}
RUN git config --global url."https://${GITHUB_TOKEN}@github.com/".insteadOf "https://github.com/"
RUN pip install -r requirements.txt

附注:这假设私有回购在requirements.txt中的格式如下:

git+https://github.com/<YOUR-USERNAME>/<YOUR-REPO>.git

对于bitbucket存储库,生成应用程序密码(bitbucket设置->访问管理->应用程序密码,见图片),具有对repo和project的读访问权限。

那么你应该使用的命令是:

git clone https://username:generated_password@bitbucket.org/reponame/projectname.git

对于其他人搜索,我有同样的问题添加-ssh默认标志使它工作