Inject host's SSH keys into Docker Machine with Docker Compose

后端 未结 6 1425
故里飘歌
故里飘歌 2020-12-13 13:00

I am using Docker on Mac OS X with Docker Machine (with the default boot2docker machine), and I use docker-compose to setup my development environment.

Let\'s say th

6条回答
  •  囚心锁ツ
    2020-12-13 13:44

    You can use multi stage build to build containers This is the approach you can take :-

    Stage 1 building an image with 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
    

    Stage 2: build your container

    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"] 
    

    add env attribute in your compose file:

    environment:
          - SSH_PRIVATE_KEY=${SSH_PRIVATE_KEY}
    

    then pass args from build script like this:

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

    And remove the intermediate container it for security. This Will help you cheers.

提交回复
热议问题