ssh - Dockerfile for cloning private git repo

ID : 274321

viewed : 36

Tags : sshdockerssh

Top 5 Answer for ssh - Dockerfile for cloning private git repo

vote vote


What's the output of the build process?

Random guess: try to chmod 600 the private key.

If it still doesn't work, try to RUN ssh -v (after adding the key); the output should explain what's happening.

vote vote


RUN ssh-keyscan >> ~/.ssh/known_hosts

The keyscan works great since it accepts the host. The following complete answer worked:

RUN mkdir -p /root/.ssh RUN cp /var/my-app/id_rsa /root/.ssh/id_rsa RUN chmod 600 /root/.ssh/id_rsa RUN ssh-keyscan >> /root/.ssh/known_hosts 

Also as mentioned:

RUN ssh -v 

^ Great way to debug the flow. That's how I realized I needed the keyscan >> known_hosts

vote vote


(Will probably not fit your needs)

There is another approach:

Go to Settings > Personal access tokens and generate a personal access token with repo scope enabled. Now you can do git clone


  • very simple approach
  • token can be easily revoked


  • if someone has access to the Dockerfile he has access to the token

To fix this, you can use an environment variable to store the token

vote vote


Below approach is using https with Personal Access Token, and it works like charm.

ARG git_personal_token RUN git config --global url."https://${git_personal_token}".insteadOf "" RUN git clone /project 

Then, supply a docker argument as below.

docker build --build-arg git_personal_token={your_token} . 

Basic idea is from

vote vote


Top 3 video Explaining ssh - Dockerfile for cloning private git repo