【问题标题】:Can not ssh-add private key to docker build无法通过 ssh 将私钥添加到 docker build
【发布时间】:2018-07-30 01:12:59
【问题描述】:

我正在尝试在构建过程中向 ssh-agent(docker 映像)添加本地私钥。

问题 我已经运行 eval$(ssh-agent -s) 并且一旦 docker 运行 ssh-add /etc/ssh/id_rsa 我收到以下错误:

Could not open a connection to your authentication agent.

目标: 我需要在 NPM 安装过程中克隆一个私有 git 存储库。这个本地密钥将允许我对私人仓库进行身份验证。

==== 输出片段 ====

Step 8/16 : RUN eval $(ssh-agent -s)
 ---> Running in 195ffeb1f84f
Agent pid 8
 ---> 0fcbc89d362f
Removing intermediate container 195ffeb1f84f
Step 9/16 : RUN ssh-add /etc/ssh/id_rsa
 ---> Running in ae99039e1fba
Could not open a connection to your authentication agent.
The command '/bin/sh -c ssh-add /etc/ssh/id_rsa' returned a non-zero code: 2

【问题讨论】:

  • 您在步骤 8 中运行的代理在您进入步骤 9 时已失效。您需要或一次性执行所有步骤才能使其正常工作。 RUN eval $(ssh-agent -s) && ssh-add /etc/ssh/id_rsa && git checkout .....
  • 我真的只是想通了。非常感谢!

标签: git docker ssh ssh-agent


【解决方案1】:

为什么不在容器中使用音量?

您可以使用/root/.ssh/id_rsa 路径将/etc/ssh/id_rsa 挂载到容器卷中。

【讨论】:

  • 您只能在运行时使用挂载卷。
  • 值得一提的是,这对~/.ssh/config的所有权存在挑战。我还没有找到解决方法。
【解决方案2】:

您在第 8 步中运行的代理在您进入第 9 步时已失效。您需要或一次性执行所有步骤才能使其正常工作。

RUN eval $(ssh-agent -s) && ssh-add /etc/ssh/id_rsa && git checkout .....

【讨论】:

  • 这是我在 dockerfile 中使用的确切代码。我也从 Alpine 镜像移到了 Node: 9 (jessie) RUN echo "$ssh_prv_key" > /etc/ssh/id_rsa && echo "$ssh_pub_key" > /etc/ssh/id_rsa.pub && chmod 600 /etc/ssh/id_rsa && chmod 600 /etc/ssh/id_rsa.pub && \ chmod 600 /etc/ssh/ && \ eval $(ssh-agent -s) && \ ssh-add /etc/ssh/id_rsa && \ ssh-add -l && \ cat /etc/ssh/id_rsa && cat /etc/ssh/id_rsa.pub && \ npm install -g sails && \ npm install
  • 谢谢!经过一天的摸索和尝试随机的事情,这救了我
  • 天啊...是的,这行得通。我一直在尝试很多不同的方法来实现这一点,看来这种方法(一次性调用所有内容)确实是实现这一目标的唯一方法。
猜你喜欢
  • 1970-01-01
  • 2018-06-25
  • 2021-11-29
  • 1970-01-01
  • 2020-06-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-21
相关资源
最近更新 更多