我有一个应用程序,执行各种有趣的东西与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构建时进行npm安装时也遇到了类似的问题。

从Daniel van Flymen的解决方案中受到启发,并将其与git url重写相结合,我们找到了一个更简单的方法来从私有github reppos中验证npm安装-我们使用oauth2令牌而不是密钥。

在我们的例子中,npm依赖项被指定为“git+https://github.com/…”

对于容器中的身份验证,url需要重写以适合ssh身份验证(ssh://git@github.com/)或令牌身份验证(https://${GITHUB_TOKEN}@github.com/)

构建命令:

docker build -t sometag --build-arg GITHUB_TOKEN=$GITHUB_TOKEN . 

不幸的是,我在docker 1.9上,所以——squash选项还没有,最终需要添加它

Dockerfile:

FROM node:5.10.0

ARG GITHUB_TOKEN

#Install dependencies
COPY package.json ./

# add rewrite rule to authenticate github user
RUN git config --global url."https://${GITHUB_TOKEN}@github.com/".insteadOf "https://github.com/"

RUN npm install

# remove the secret token from the git config file, remember to use --squash option for docker build, when it becomes available in docker 1.13
RUN git config --global --unset url."https://${GITHUB_TOKEN}@github.com/".insteadOf

# Expose the ports that the app uses
EXPOSE 8000

#Copy server and client code
COPY server /server 
COPY clients /clients

其他回答

正如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

注意:只使用这种方法的图像是私有的,将永远是!

ssh密钥仍然存储在映像中,即使您在添加密钥后在层命令中删除密钥(参见本文中的评论)。

在我的情况下,这是可以的,所以这是我使用的:

# Setup for ssh onto github
RUN mkdir -p /root/.ssh
ADD id_rsa /root/.ssh/id_rsa
RUN chmod 700 /root/.ssh/id_rsa
RUN echo "Host github.com\n\tStrictHostKeyChecking no\n" >> /root/.ssh/config

从docker API 1.39+(检查API版本和docker版本)开始,docker构建允许——ssh选项与代理套接字或键一起允许docker引擎转发ssh代理连接。

建造的命令

export DOCKER_BUILDKIT=1
docker build --ssh default=~/.ssh/id_rsa .

Dockerfile

# syntax=docker/dockerfile:experimental
FROM python:3.7

# Install ssh client (if required)
RUN apt-get update -qq
RUN apt-get install openssh-client -y

# Download public key for github.com
RUN --mount=type=ssh 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

更多信息:

https://docs.docker.com/develop/develop-images/build_enhancements/#using-ssh-to-access-private-data-in-builds https://github.com/moby/buildkit/blob/master/frontend/dockerfile/docs/experimental.md#run---mounttypessh

一个简单而安全的方法来实现这一点,而不需要将密钥保存在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" ]

一个跨平台的解决方案是使用绑定挂载将主机的.ssh文件夹共享给容器:

docker run -v /home/<host user>/.ssh:/home/<docker user>/.ssh <image>

与代理转发类似,这种方法将使容器可以访问公钥。另外一个好处是,它也适用于非根用户,并将使您连接到GitHub。但是,需要注意的一点是,.ssh文件夹中的所有内容(包括私钥)都将被共享,因此这种方法只适用于开发,而且只适用于受信任的容器映像。