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


当前回答

您可以使用多级构建来构建容器 你可以这样做:-

阶段1使用ssh构建映像

FROM ubuntu as sshImage
LABEL stage=sshImage
ARG SSH_PRIVATE_KEY
WORKDIR /root/temp

RUN apt-get update && \
    apt-get install -y git npm 

RUN mkdir /root/.ssh/ &&\
    echo "${SSH_PRIVATE_KEY}" > /root/.ssh/id_rsa &&\
    chmod 600 /root/.ssh/id_rsa &&\
    touch /root/.ssh/known_hosts &&\
    ssh-keyscan github.com >> /root/.ssh/known_hosts

COPY package*.json ./

RUN npm install

RUN cp -R node_modules prod_node_modules

阶段2:构建容器

FROM node:10-alpine

RUN mkdir -p /usr/app

WORKDIR /usr/app

COPY ./ ./

COPY --from=sshImage /root/temp/prod_node_modules ./node_modules

EXPOSE 3006

CMD ["npm", "run", "dev"] 

在你的合成文件中添加env属性:

   environment:
      - SSH_PRIVATE_KEY=${SSH_PRIVATE_KEY}

然后像这样从构建脚本传递参数:

docker-compose build --build-arg SSH_PRIVATE_KEY="$(cat ~/.ssh/id_rsa)"

并移除中间容器,以确保安全。 这将帮助你干杯。

其他回答

最简单的方法是,获得一个发射台帐户并使用:ssh-import-id

这是现在可用的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

你也可以在主机和容器之间链接你的.ssh目录,我不知道这种方法是否有任何安全隐患,但它可能是最简单的方法。像这样的东西应该工作:

$ sudo docker run -it -v /root/.ssh:/root/.ssh someimage bash

记住docker使用sudo运行(除非你不这样做),如果是这种情况,你将使用根ssh密钥。

你可以通过共享文件夹将授权密钥传递到你的容器中,并使用docker文件设置权限,如下所示:

FROM ubuntu:16.04
RUN apt-get install -y openssh-server
RUN mkdir /var/run/sshd
EXPOSE 22
RUN cp /root/auth/id_rsa.pub /root/.ssh/authorized_keys
RUN rm -f /root/auth
RUN chmod 700 /root/.ssh
RUN chmod 400 /root/.ssh/authorized_keys
RUN chown root. /root/.ssh/authorized_keys
CMD /usr/sbin/sshd -D

您的docker运行包含如下内容,与容器共享主机上的认证目录(持有authorised_keys),然后打开ssh端口,该端口将通过主机上的端口7001访问。

-d -v /home/thatsme/dockerfiles/auth:/root/auth -–publish=127.0.0.1:7001:22

您可能想要查看https://github.com/jpetazzo/nsenter,它似乎是打开容器上的shell并在容器中执行命令的另一种方式。

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