How to deploy code from Github using a deployment key in Docker?

I want to output code from Github to a Docker image during its construction. I have a deployment key created in the repository, but it seems to me that ssh-agent is not working on my Docker image.

What I did (my Docker file):

FROM python:2.7-stretch
ADD ./id_rsa /root/.ssh/id_rsa
RUN eval "$(ssh-agent -s)"
RUN ssh-add -K /root/.ssh/id_rsa

Conclusion:

Step 12/22 : RUN eval "$(ssh-agent -s)"
 ---> Running in f9ad80981cee
Agent pid 6
Removing intermediate container f9ad80981cee
 ---> d773f7ce5917
Step 13/22 : RUN ssh-add -K /root/.ssh/id_rsa
 ---> Running in 95efeed6a7ad
Could not open a connection to your authentication agent.
The command '/bin/sh -c ssh-add -K /root/.ssh/id_rsa' returned a non-zero code: 2

As you can see, ssh-agent is running, but keys are not added to it.

If I skip the ssh-add step, then my git pull will fail later due to privileges that do not work as expected, as authentication failed.

+4
source share
3 answers

In fact, you do not need to copy your secret key into your container (and you better not do this).

, , ssh-agent : , , , - ssh-aget:

docker-compose:

environment:
  - "SSH_AUTH_SOCK=/tmp/ssh-agent"
volumes:
  - $SSH_AUTH_SOCK:/tmp/ssh-agent

:

docker run -v $SSH_AUTH_SOCK:/tmp/ssh-agent -e SSH_AUTH_SOCK=/tmp/ssh-agent

P.S

, , export, evaled ssh-agent.

: SSH_AUTH_SOCK SSH_AGENT_PID. export .

RUN : ssh-agent , . Dockerfile ( ).

(, ), RUN:

RUN eval "$(ssh-agent -s)" && ssh-add /root/.ssh/id_rsa
0

:

-K - ssh-add, Apple, , ssh SSH-.

Apple, .

-K . :

FROM python:2.7-stretch
ADD ./id_rsa /root/.ssh/id_rsa
RUN eval "$(ssh-agent -s)" && ssh-add /root/.ssh/id_rsa
0

Edit ~/.ssh/config

Add using a new key

Host github.com
IdentityFile /root/.ssh/id_rsa
-1
source

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


All Articles