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

其他回答

以下是Docker容器中SSH所面临的挑战的简要概述。要从容器内连接到可信的远程而不泄漏机密,有以下几种方法:

SSH代理转发(只支持linux,不是直接的) 内置SSH,使用BuildKit(实验性,Compose还不支持) 使用绑定挂载来公开~/。SSH到容器。(仅限开发,潜在不安全) Docker Secrets(跨平台,增加复杂性)

除此之外,在使用Compose时,还可以使用在运行时可访问的独立docker容器中运行的密钥存储。这里的缺点是由于创建和管理密钥存储库(如HashiCorp的Vault)所需的机制而增加了复杂性。

对于在独立的Docker容器中使用SSH密钥,请参阅上面链接的方法,并根据您的具体需求考虑每种方法的缺点。然而,如果你在Compose中运行,并且想要在运行时共享一个应用程序的密钥(反映OP的实用性)试试这个:

创建一个docker-compose。Env文件并将其添加到.gitignore文件中。 更新docker-compose。Yml并为需要密钥的服务添加env_file。 在应用程序运行时从环境中访问公钥,例如process.node。Node.js应用中的DEPLOYER_RSA_PUBKEY。

上述方法非常适合开发和测试,虽然它可以满足生产需求,但在生产中,您最好使用上述其他方法之一。

额外的资源:

Docker文档:使用绑定挂载 Docker Docs:使用Docker机密管理敏感数据 堆栈溢出:在docker容器内使用SSH密钥 Stack Overflow:在macOS上使用ssh-agent和docker

如果你正在使用Docker Compose,一个简单的选择是像这样转发SSH代理:

something:
    container_name: something
    volumes:
        - $SSH_AUTH_SOCK:/ssh-agent # Forward local machine SSH key to docker
    environment:
        SSH_AUTH_SOCK: /ssh-agent

或者等效地,如果使用docker run:

$ docker run --mount type=bind,source=$SSH_AUTH_SOCK,target=/ssh-agent \
             --env SSH_AUTH_SOCK=/ssh-agent \
             some-image

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

下面是我如何在使用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

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

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