我有一个应用程序,执行各种有趣的东西与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 run -it -v ~/.ssh/id_rsa:/root/.my-key:ro image /bin/bash

(注意,readonly标志,所以容器不会乱我的ssh密钥在任何情况下。)

在容器内,我现在可以运行:

ssh-agent bash -c "ssh-add ~/.my-key; git clone <gitrepourl> <target>"

所以我不会得到/root/。ssh/。@kross注意到了这个错误

其他回答

您可以使用secrets来管理容器中的任何敏感数据 在运行时需要,但您不想存储在映像或源代码中 控件,例如: 用户名及密码 TLS证书和密钥 SSH密钥 其他重要数据,如数据库或内部服务器的名称 通用字符串或二进制内容(大小不超过500kb) https://docs.docker.com/engine/swarm/secrets/

我试图弄清楚如何将签名密钥添加到容器中,以便在运行时(而不是构建)使用,然后遇到了这个问题。Docker secrets似乎是我用例的解决方案,由于还没有人提到它,我将添加它。

我们在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

在一个运行的docker容器中,你可以使用docker -i (interactive)选项发出ssh-keygen。这将转发容器提示,在docker容器中创建密钥。

你可以通过共享文件夹将授权密钥传递到你的容器中,并使用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并在容器中执行命令的另一种方式。

对于debian / root / authorized_keys:

RUN set -x && apt-get install -y openssh-server

RUN mkdir /var/run/sshd
RUN mkdir -p /root/.ssh
RUN sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config
RUN  echo "ssh-rsa AAAA....yP3w== rsa-key-project01" >> /root/.ssh/authorized_keys
RUN chmod -R go= /root/.ssh