首页 > 解决方案 > 将私有 ssh 密钥添加到 docker

问题描述

我正在尝试将我的 ssh 私钥添加到 docker,我的 Dockerfile 如下:

ARG key
RUN echo $key > panaxea-key

RUN chmod 600 panaxea-key
RUN eval $(ssh-agent -s) && ssh-add panaxea-key

控制台输出:

Step 10/14 : RUN echo $key > panaxea-key
 ---> Using cache
 ---> 8ee023dba45e
Step 11/14 : RUN chmod 600 panaxea-key
 ---> Using cache
 ---> b7c42a9c5268
Step 12/14 : RUN eval $(ssh-agent -s) && ssh-add panaxea-key
 ---> Running in 9895e087cea5
Agent pid 9
Enter passphrase for panaxea-key: The command '/bin/sh -c eval $(ssh-agent -s) && ssh-add panaxea-key' returned a non-zero code: 1
dario@dario-X750JB:~/Panaxea_Docker$ 

有什么建议吗?我的密钥没有密码

标签: dockerubuntu

解决方案


如果您的密钥没有密码并且 ssh-add 要求您提供密码,这意味着 ssh-add 无法理解您的密钥文件。罪魁祸首是echo $key > panaxea-key。您会看到该文件现在是一行文本,并且所有换行符都消失了。

改为使用echo "$key" > panaxea-key

这应该可以让它工作,但是关于在 docker 镜像中嵌入私钥的警告仍然存在。你如何解决这个问题取决于你想要完成什么,但我觉得这完全是一个不同的问题。


推荐阅读