5

I'm trying to clone a private repo from github using Docker. The problem is that I need to use ssh to access to that repo. I added a key in my github project's setting which is used, I suppose, to identify the docker's server.

My problem is that I can't figure out what I should write in my Dockerfile so that the server use that key when it tries to access to my github repo. I saw examples where id_rsa is added to the container, but I don't know where id_rsa is stored on their server, if it exists

RUN mkdir /root/.ssh/
# can't find the file id_rsa
ADD id_rsa /root/.ssh/id_rsa 
RUN touch /root/.ssh/known_hosts
RUN ssh-keyscan github.com >> /root/.ssh/known_hosts

run git clone [email protected]:user/repo.git

How do I access to my private repo from docker's server ?

0

2 Answers 2

10

I managed to do this by using ssh-add on the key.

A "problem" with using multiple RUN instructions is that non-persistent data won't be available at the next RUN. I managed to log in and use github private repos with

RUN eval `ssh-agent -s` && \
    ssh-add id_rsa && \
    git clone [email protected]:user/repo.git

By using the &&'s on a single CMD, the eval process will still be running and ssh-agent has the key loaded when you connect to github.

I hope this helps.

Sign up to request clarification or add additional context in comments.

3 Comments

As I said, I don't know where id_rsa is on their server, the problem isn't building on my computer, it's building on their server
If you use ADD it should be the same path as the Dockerfile
I believe doing it in this manner will include your id_rsa key within your docker cache until you remove them. I realize the OP is showing this as well but it might not be a good idea to add your id_rsa key to a docker container nonetheless.
5

If you want to use git from the Dockerfile, you need to configure the container in the same way as you would configure your own development machine.

I can't really understand which is "their" in "their server", so I'm just guessing.

When you registered a public key with github, you should have got the private key too. That's the id_rsa that you see in other examples.

If you can not locate that file, just start over. Delete the old key, generate a new one and configure it on github and on your docker build context (in your example, just copy it in the same folder as the Dockerfile).

As a different strategy, you might want to do the checkout outside of the image build process (clone locally and ADD everything to the image).

1 Comment

I didn't know about the building context, I thought ADD searched the file on the host directly, that's also the reason why I used git clone in the Dockerfile which was useless since, as you said, I just had to add the directory where the Dockerfile was located. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.