Usman Zahid
Usman Zahid

Reputation: 31

Git Clone using SSH in Dockerfile

I am trying to clone a git repository using ssh in a docker image and i want to do this while building the dockerfile. I can make this work using git clone https with username and password, but using ssh, it fails. My dockerfile looks like

# Install software 
RUN apt-get install -y git
# Make ssh dir
RUN mkdir -p /${APP_USER}/.ssh/

RUN apt-get update
RUN apt-get -qq -y install curl
RUN apt-get -qq -y install \
  openssh-client openssh-server
ADD id_rsa /${APP_USER}/.ssh/id_rsa

# Create known_hosts
RUN touch /${APP_USER}/.ssh/known_hosts
# Add bitbuckets key
RUN ssh-keyscan bitbucket.org >> /${APP_USER}/.ssh/known_hosts

# Pull the master branch 
RUN cd ${HOME} \
 && git clone ssh://[email protected]...... \
 && ls -la


I have added steps to install git, copied the private ssh key to the user folder, placed the public key in bitbucket repo, added known hosts but still i get the following error

Host key verification failed.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.


Upvotes: 2

Views: 4089

Answers (1)

VonC
VonC

Reputation: 1328342

Add a RUN ssh -Tv [email protected] to check for any error message.

I would suspect for instance a chmod issue

RUN \
    chmod 700 $HOME/.ssh &&\
    chmod 600 $HOME/.ssh/id_rsa 

As commented, ssh-keyscan (if you have installed it) can be needed, to complete the ssh/known_hosts file. For instance:

# Authorize SSH Host
RUN mkdir -p /root/.ssh && \
    chmod 0700 /root/.ssh && \
    ssh-keyscan example.com > /root/.ssh/known_hosts && \
    # Add the keys and set permissions
    echo "$PRIVATE_SSH_KEY" > /root/.ssh/id_rsa && \
    chmod 600 /root/.ssh/id_rsa

Upvotes: 3

Related Questions