我有一个应用程序,执行各种有趣的东西与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" ]

其他回答

这一行是个问题:

ADD ../../home/ubuntu/.ssh/id_rsa /root/.ssh/id_rsa

当指定要复制到映像中的文件时,只能使用相对路径——相对于Dockerfile所在的目录。所以你应该用:

ADD id_rsa /root/.ssh/id_rsa

将id_rsa文件放到Dockerfile所在的目录中。

查看更多详细信息:http://docs.docker.io/reference/builder/#add

下面是我如何在使用docker composer构建图像时使用ssh键:

.env

SSH_PRIVATE_KEY=[base64 encoded sshkey]

docker-compose.yml

version: '3'
services:
  incatech_crawler:
    build:
      context: ./
      dockerfile: Dockerfile
      args:
        SSH_PRIVATE_KEY: ${SSH_PRIVATE_KEY} 

dockerfile: ...

# Set the working directory to /app
WORKDIR /usr/src/app/
ARG SSH_PRIVATE_KEY 
 
RUN mkdir /root/.ssh/  
RUN echo -n ${SSH_PRIVATE_KEY} | base64 --decode > /root/.ssh/id_rsa_wakay_user

在使用Ubuntu时,ssh_config是不正确的。你需要加上

RUN  echo "    IdentityFile ~/.ssh/id_rsa" >> /etc/ssh/ssh_config

来让Dockerfile识别你的ssh密钥。

扩展Peter Grainger的回答,我可以使用Docker 17.05以来可用的多级构建。官方页面说明:

对于多阶段构建,您可以在Dockerfile中使用多个FROM语句。每个FROM指令都可以使用不同的基,并且每个FROM指令都开始构建的一个新阶段。您可以有选择地将工件从一个阶段复制到另一个阶段,在最终图像中留下您不想要的所有东西。

请记住这一点,这里是我的Dockerfile示例,包括三个构建阶段。它的目的是创建客户端web应用程序的生产映像。

# Stage 1: get sources from npm and git over ssh
FROM node:carbon AS sources
ARG SSH_KEY
ARG SSH_KEY_PASSPHRASE
RUN mkdir -p /root/.ssh && \
    chmod 0700 /root/.ssh && \
    ssh-keyscan bitbucket.org > /root/.ssh/known_hosts && \
    echo "${SSH_KEY}" > /root/.ssh/id_rsa && \
    chmod 600 /root/.ssh/id_rsa
WORKDIR /app/
COPY package*.json yarn.lock /app/
RUN eval `ssh-agent -s` && \
    printf "${SSH_KEY_PASSPHRASE}\n" | ssh-add $HOME/.ssh/id_rsa && \
    yarn --pure-lockfile --mutex file --network-concurrency 1 && \
    rm -rf /root/.ssh/

# Stage 2: build minified production code
FROM node:carbon AS production
WORKDIR /app/
COPY --from=sources /app/ /app/
COPY . /app/
RUN yarn build:prod

# Stage 3: include only built production files and host them with Node Express server
FROM node:carbon
WORKDIR /app/
RUN yarn add express
COPY --from=production /app/dist/ /app/dist/
COPY server.js /app/
EXPOSE 33330
CMD ["node", "server.js"]

.dockerignore重复.gitignore文件的内容(它防止项目的node_modules和结果dist目录被复制):

.idea
dist
node_modules
*.log

创建镜像的命令示例:

$ docker build -t ezze/geoport:0.6.0 \
  --build-arg SSH_KEY="$(cat ~/.ssh/id_rsa)" \
  --build-arg SSH_KEY_PASSPHRASE="my_super_secret" \
  ./

如果您的私钥没有密码短语,请指定空的SSH_KEY_PASSPHRASE参数。

它是这样工作的:

1).只对第一级包装。json,纱线。锁文件和私有SSH密钥被复制到第一个名为sources的中间映像。为了避免进一步的SSH密钥口令提示,它被自动添加到SSH -agent。最后yarn命令从NPM安装所有需要的依赖项,并通过SSH从Bitbucket克隆私有git存储库。

2).第二阶段构建并缩小web应用程序的源代码,并将其放置在下一个名为production的中间映像的dist目录中。注意,安装的node_modules的源代码是从第一阶段生成的名为sources的镜像中复制的:

COPY --from=sources /app/ /app/

也可能是下面这句话:

COPY --from=sources /app/node_modules/ /app/node_modules/

我们只有来自第一个中间映像的node_modules目录,不再有SSH_KEY和SSH_KEY_PASSPHRASE参数。构建所需的所有其余内容都是从项目目录中复制的。

3).在第三阶段,我们减少最终图像的大小,将标记为ezze/geoport:0.6.0,只包括dist目录从第二个中间图像名为生产和安装Node Express启动一个web服务器。

列出图像给出如下输出:

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ezze/geoport        0.6.0               8e8809c4e996        3 hours ago         717MB
<none>              <none>              1f6518644324        3 hours ago         1.1GB
<none>              <none>              fa00f1182917        4 hours ago         1.63GB
node                carbon              b87c2ad8344d        4 weeks ago         676MB

其中非标记图像对应于第一和第二个中间构建阶段。

如果你跑了

$ docker history ezze/geoport:0.6.0 --no-trunc

在最终的映像中,您不会看到任何提及SSH_KEY和SSH_KEY_PASSPHRASE的内容。

为了在容器中注入ssh密钥,你有多种解决方案:

使用带有ADD指令的Dockerfile,你可以在构建过程中注入它 简单地执行cat id_rsa | docker run -i <image> sh -c 'cat > /root/.ssh/id_rsa' 使用docker cp命令,允许您在容器运行时注入文件。