Usama
Usama

Reputation: 480

add ssh keys in Docker

in my Dockefile i am adding ssh key to the docker and cloning a project from bitbucket. i can pull another branch in the Docker file easily.

ARG key
ARG pub_key
RUN mkdir /root/.ssh/
RUN ssh-keyscan bitbucket.org >> /root/.ssh/known_hosts
ADD $key /root/.ssh/
ADD $pub_key /root/.ssh/
RUN git clone [email protected]:******************/sql.git
WORKDIR "/sql"
RUN git pull origin testBranch

the repo is cloned sucesfully and and a pull is made successfully from the testBranch when i run this docker using docker run command and try any git command it says

Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights

but the ssh key is present in the directory /root/.ssh

Upvotes: 6

Views: 3091

Answers (2)

Mohammad Azim
Mohammad Azim

Reputation: 2973

Instead of passing keys as args you can also mount host's .ssh directory into docker with following options:

docker run -v /home/<host user>/.ssh:/home/<docker user>/.ssh <image>

Upvotes: 1

Sajib Khan
Sajib Khan

Reputation: 24204

Your key is added against root user (/root/.ssh/). I guess when you run docker run you are switching to another user (maybe, docker).

If you add your id_rsa.pub to correct user (the user after running docker run command) then it should work.

$ whoami show you the current user.

Upvotes: 2

Related Questions