使用 dockerfile 克隆私有 git 存储库

新手上路,请多包涵

我已经从周围的各种工作 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

可悲的是,这也行不通。

原文由 crooksey 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 668
2 个回答

我的密钥受密码保护,这导致了问题,下面列出了一个工作文件(为未来的谷歌用户提供帮助)

 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

原文由 crooksey 发布,翻译遵循 CC BY-SA 4.0 许可协议

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

  1. 使用 ssh-keygen -q -t rsa -N '' -f repo-key 生成 ssh 密钥,这将为您提供 repo-key 和 repo-key.pub 文件。

  2. 将 repo-key.pub 添加到您的存储库部署密钥。

在 GitHub 上,转到 [您的存储库] -> 设置 -> 部署密钥

  1. 在 Dockerfile 中添加类似这样的内容:
   添加回购密钥/
   跑 \
     chmod 600 /repo-key && \
     echo "IdentityFile /repo-key" >> /etc/ssh/ssh_config && \
     echo -e "StrictHostKeyChecking no" >> /etc/ssh/ssh_config && \
     // 你的 git clone 命令在这里...

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

原文由 Marcin R 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题