Add ssh keys to Docker

in my Dockefile, I add the ssh key to the docker and clone the project from the bitpack. I can easily extract another branch to the Docker file.

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 git@bitbucket.org :******************/sql.git WORKDIR "/sql" RUN git pull origin testBranch 

the repo is cloned with a sufficient degree of accuracy, and the attraction is successfully executed from testBranch when I launch this docker using the docker run and try any git command that 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 in the /root/.ssh directory

+5
source share
2 answers

The key is added to the root user ( /root/.ssh/ ). I think when you start docker run , you switch to another user (maybe dockers ).

If you add id_rsa.pub for the correct user (the user after running the docker run ), it should work.

$ whoami will show you the current user.

+2
source

Instead of passing keys as arguments, you can also connect the host .ssh directory to the docker with the following parameters:

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

Source: https://habr.com/ru/post/1268425/


All Articles