我有一个应用程序,执行各种有趣的东西与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 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
您可以使用多级构建来构建容器
你可以这样做:-
阶段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)"
并移除中间容器,以确保安全。
这将帮助你干杯。
这个问题真的很烦人。由于您不能在dockerfile上下文中添加/复制任何文件,这意味着不可能只链接~/。Ssh /id_rsa到镜像的/root/目录下。Ssh /id_rsa,当你确实需要一个密钥来做一些事情,比如从一个私有的repo链接克隆git…,在构建docker映像期间。
不管怎样,我找到了一个变通的办法,不那么有说服力,但对我来说确实有效。
在dockerfile中:
将此文件添加为/root/.ssh/id_rsa
做你想做的,比如git克隆,作曲家…
rm /root/.Ssh /id_rsa
一次拍摄的剧本:
Cp你的密钥文件夹持有dockerfile
码头工人建造
Rm复制的密钥
任何时候你需要从这个镜像运行一个有SSH要求的容器,只要为运行命令添加-v就可以了,比如:
Docker运行-v ~/.ssh/id_rsa:/root/。Ssh /id_rsa——name容器镜像命令
这个解决方案在项目源代码和构建的docker映像中都没有私钥,因此不再需要担心安全问题。
这是现在可用的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