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
We had similar problem when doing npm install in docker build time.
Inspired from solution from Daniel van Flymen and combining it with git url rewrite, we found a bit simpler method for authenticating npm install from private github repos - we used oauth2 tokens instead of the keys.
In our case, the npm dependencies were specified as "git+https://github.com/..."
For authentication in container, the urls need to be rewritten to either be suitable for ssh authentication (ssh://git@github.com/) or token authentication (https://${GITHUB_TOKEN}@github.com/)
Build command:
Unfortunately, I'm on docker 1.9, so --squash option is not there yet, eventually it needs to be added
Dockerfile:
I'm trying to work the problem the other way: adding public ssh key to an image. But in my trials, I discovered that "docker cp" is for copying FROM a container to a host. Item 3 in the answer by creak seems to be saying you can use docker cp to inject files into a container. See https://docs.docker.com/engine/reference/commandline/cp/
excerpt
It's a harder problem if you need to use SSH at build time. For example if you're using
git clone
, or in my casepip
andnpm
to download from a private repository.The solution I found is to add your keys using the
--build-arg
flag. Then you can use the new experimental--squash
command (added 1.13) to merge the layers so that the keys are no longer available after removal. Here's my solution:Build command
Dockerfile
Update: If you're using Docker 1.13 and have experimental features on you can append
--squash
to the build command which will merge the layers, removing the SSH keys and hiding them fromdocker history
.The ssh key remains stored within the image, even if you remove the key in a layer command after adding it (see comments in this post).
In my case this is ok, so this is what I am using:
I ran into the same problem today and little bit modified version with previous posts I found this approach more useful to me
(Note that readonly flag so container will not mess my ssh key in any case.)
Inside container I can now run:
So I don't get that
Bad owner or permissions on /root/.ssh/..
error which was noted by @krossYou can pass the authorised keys in to your container using a shared folder and set permissions using a docker file like this:
And your docker run contains something like the following to share an auth directory on the host (holding the authorised_keys) with the container then open up the ssh port which will be accessable through port 7001 on the host.
You may want to look at https://github.com/jpetazzo/nsenter which appears to be another way to open a shell on a container and execute commands within a container.