gitdockersshssh-agent

Can not ssh-add private key to docker build


I'm trying to add a local private key to the ssh-agent (docker image) during the build process.

Problem I've run eval $(ssh-agent -s) and once docker runs the ssh-add /etc/ssh/id_rsa I receive the following error:

Could not open a connection to your authentication agent.

Goal: I need to clone a private git repo during to NPM install process. This local key will allow me to authenticate against the private repo.

==== Output Snippet ====

Step 8/16 : RUN eval $(ssh-agent -s)
 ---> Running in 195ffeb1f84f
Agent pid 8
 ---> 0fcbc89d362f
Removing intermediate container 195ffeb1f84f
Step 9/16 : RUN ssh-add /etc/ssh/id_rsa
 ---> Running in ae99039e1fba
Could not open a connection to your authentication agent.
The command '/bin/sh -c ssh-add /etc/ssh/id_rsa' returned a non-zero code: 2

Solution

  • The agent you run in Step 8 is dead by the time you are in Step 9. You need or perform all steps in one go for this to work.

    RUN eval $(ssh-agent -s) && ssh-add /etc/ssh/id_rsa && git checkout .....