I have an app that executes various fun stuff with Git (like running git clone & git push) and I'm trying to docker-ize it.
I'm running into an issue though where I need to be able to add an SSH key to the container for the container 'user' to use.
I tried copying it into /root/.ssh/
, changing $HOME
, creating a git ssh wrapper, and still no luck.
Here is the Dockerfile for reference:
#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
runs the git commands like git pull
One solution is to mount host's ssh keys into docker with following options:
Similar to above solution. But works with a non root user. Work perfectly with github.
In later versions of docker (17.05) you can use multi stage builds. Which is the safest option as the previous builds can only ever be used by the subsequent build and are then destroyed
See the answer to my stackoverflow question for more info
Expanding Peter Grainger's answer I was able to use multi-stage build available since Docker 17.05. Official page states:
Keeping this in mind here is my example of
Dockerfile
including three build stages. It's meant to create a production image of client web application..dockerignore
repeats contents of.gitignore
file (it preventsnode_modules
and resultingdist
directories of the project from being copied):Command example to build an image:
If your private SSH key doesn't have a passphrase just specify empty
SSH_KEY_PASSPHRASE
argument.This is how it works:
1). On the first stage only
package.json
,yarn.lock
files and private SSH key are copied to the first intermediate image namedsources
. In order to avoid further SSH key passphrase prompts it is automatically added tossh-agent
. Finallyyarn
command installs all required dependencies from NPM and clones private git repositories from Bitbucket over SSH.2). The second stage builds and minifies source code of web application and places it in
dist
directory of the next intermediate image namedproduction
. Note that source code of installednode_modules
is copied from the image namedsources
produced on the first stage by this line:Probably it also could be the following line:
We have only
node_modules
directory from the first intermediate image here, noSSH_KEY
andSSH_KEY_PASSPHRASE
arguments anymore. All the rest required for build is copied from our project directory.3). On the third stage we reduce a size of the final image that will be tagged as
ezze/geoport:0.6.0
by including onlydist
directory from the second intermediate image namedproduction
and installing Node Express for starting a web server.Listing images gives an output like this:
where non-tagged images correpsond to the first and the second intermediate build stages.
If you run
you will not see any mentions of
SSH_KEY
andSSH_KEY_PASSPHRASE
in the final image.Turns out when using Ubuntu, the ssh_config isn't correct. You need to add
to your Dockerfile in order to get it to recognize your ssh key.
If you are using docker compose an easy choice is to forward SSH agent like that:
This line is a problem:
When specifying the files you want to copy into the image you can only use relative paths - relative to the directory where your Dockerfile is. So you should instead use:
And put the id_rsa file into the same directory where your Dockerfile is.
Check this out for more details: http://docs.docker.io/reference/builder/#add