无法通过 ssh 将私钥添加到 docker 构建

Can not ssh-add private key to docker build

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

问题 我已经 运行 eval$(ssh-agent -s) 并且一旦 docker 运行s 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

为什么不在容器中使用卷?

您可以使用 /root/.ssh/id_rsa 路径在容器卷中装载 /etc/ssh/id_rsa

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

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