我有一个应用程序,执行各种有趣的东西与Git(像运行Git克隆& Git推送),我试图docker-ize它。
我遇到了一个问题,虽然我需要能够添加一个SSH密钥到容器的容器“用户”使用。
我试着把它复制到/root/。ssh/,更改$HOME,创建一个git ssh包装器,仍然没有运气。
以下是Dockerfile供参考:
#DOCKER-VERSION 0.3.4
from ubuntu:12.04
RUN apt-get update
RUN apt-get install python-software-properties python g++ make git-core openssh-server -y
RUN add-apt-repository ppa:chris-lea/node.js
RUN echo "deb http://archive.ubuntu.com/ubuntu precise universe" >> /etc/apt/sources.list
RUN apt-get update
RUN apt-get install nodejs -y
ADD . /src
ADD ../../home/ubuntu/.ssh/id_rsa /root/.ssh/id_rsa
RUN cd /src; npm install
EXPOSE 808:808
CMD [ "node", "/src/app.js"]
App.js运行git命令,比如git pull
一个简单而安全的方法来实现这一点,而不需要将密钥保存在Docker镜像层中,或通过ssh_agent体操:
As one of the steps in your Dockerfile, create a .ssh directory by adding:
RUN mkdir -p /root/.ssh
Below that indicate that you would like to mount the ssh directory as a volume:
VOLUME [ "/root/.ssh" ]
Ensure that your container's ssh_config knows where to find the public keys by adding this line:
RUN echo " IdentityFile /root/.ssh/id_rsa" >> /etc/ssh/ssh_config
Expose you local user's .ssh directory to the container at runtime:
docker run -v ~/.ssh:/root/.ssh -it image_name
Or in your dockerCompose.yml add this under the service's volume key:
- "~/.ssh:/root/.ssh"
你最终的Dockerfile应该包含如下内容:
FROM node:6.9.1
RUN mkdir -p /root/.ssh
RUN echo " IdentityFile /root/.ssh/id_rsa" >> /etc/ssh/ssh_config
VOLUME [ "/root/.ssh" ]
EXPOSE 3000
CMD [ "launch" ]
正如eczajk已经在Daniel van Flymen的回答中评论的那样,删除键并使用-squash似乎不安全,因为它们仍然会在历史记录中可见(docker history -no-trunc)。
在Docker 18.09中,你可以使用“构建秘密”特性。在我的情况下,我克隆了一个私人git回购使用我的主机SSH密钥与以下在我的Dockerfile:
# syntax=docker/dockerfile:experimental
[...]
RUN --mount=type=ssh git clone [...]
[...]
为了能够使用这个,你需要在运行docker build之前启用新的BuildKit后端:
export DOCKER_BUILDKIT=1
你需要在docker build中添加——ssh default参数。
更多信息请点击:https://medium.com/@tonistiigi/build- secrets.and -ssh- foring-in dock-18-09-ae8161d066
我把一个非常简单的解决方案放在一起,它适用于我的用例,我使用一个“builder”docker映像来构建一个单独部署的可执行文件。换句话说,我的“构建器”映像永远不会离开我的本地机器,并且只需要在构建阶段访问私有回购/依赖。
对于这个解决方案,您不需要更改Dockerfile。
运行容器时,挂载~/。SSH目录(这避免了必须将键直接烘焙到映像中,而是确保它们在构建阶段只在短时间内对单个容器实例可用)。在我的例子中,我有几个构建脚本来自动化我的部署。
在我的build-and-package.sh脚本中,我像这样运行容器:
# do some script stuff before
...
docker run --rm \
-v ~/.ssh:/root/.ssh \
-v "$workspace":/workspace \
-w /workspace builder \
bash -cl "./scripts/build-init.sh $executable"
...
# do some script stuff after (i.e. pull the built executable out of the workspace, etc.)
build-init.sh脚本如下所示:
#!/bin/bash
set -eu
executable=$1
# start the ssh agent
eval $(ssh-agent) > /dev/null
# add the ssh key (ssh key should not have a passphrase)
ssh-add /root/.ssh/id_rsa
# execute the build command
swift build --product $executable -c release
因此,我们不是直接在docker run命令中执行swift构建命令(或任何与您的环境相关的构建命令),而是执行build-init.sh脚本,该脚本启动ssh-agent,然后将ssh密钥添加到代理,最后执行swift构建命令。
注1:为了使其工作,您需要确保您的ssh密钥没有密码短语,否则ssh-add /root/。Ssh /id_rsa行将要求口令并中断构建脚本。
注意2:确保在脚本文件上设置了适当的文件权限,以便它们可以运行。
希望这为具有类似用例的其他人提供了一个简单的解决方案。
这是现在可用的18.09版!
根据文档:
docker构建有一个——ssh选项来允许docker引擎
转发SSH代理连接。
下面是Dockerfile在容器中使用SSH的例子:
# syntax=docker/dockerfile:experimental
FROM alpine
# Install ssh client and git
RUN apk add --no-cache openssh-client git
# Download public key for github.com
RUN mkdir -p -m 0600 ~/.ssh && ssh-keyscan github.com >> ~/.ssh/known_hosts
# Clone private repository
RUN --mount=type=ssh git clone git@github.com:myorg/myproject.git myproject
创建Dockerfile后,使用——ssh选项连接ssh代理:
$ docker build --ssh default .
另外,请查看https://medium.com/@tonistiigi/build- secrets.and -ssh-forwarding-in dock-18-09-ae8161d066
这个问题真的很烦人。由于您不能在dockerfile上下文中添加/复制任何文件,这意味着不可能只链接~/。Ssh /id_rsa到镜像的/root/目录下。Ssh /id_rsa,当你确实需要一个密钥来做一些事情,比如从一个私有的repo链接克隆git…,在构建docker映像期间。
不管怎样,我找到了一个变通的办法,不那么有说服力,但对我来说确实有效。
在dockerfile中:
将此文件添加为/root/.ssh/id_rsa
做你想做的,比如git克隆,作曲家…
rm /root/.Ssh /id_rsa
一次拍摄的剧本:
Cp你的密钥文件夹持有dockerfile
码头工人建造
Rm复制的密钥
任何时候你需要从这个镜像运行一个有SSH要求的容器,只要为运行命令添加-v就可以了,比如:
Docker运行-v ~/.ssh/id_rsa:/root/。Ssh /id_rsa——name容器镜像命令
这个解决方案在项目源代码和构建的docker映像中都没有私钥,因此不再需要担心安全问题。