Git clone private repo inside a docker container

Viewed 1444

I have a docker container, and inside this docker container I have a shell script which performs a git clone from a private repo. The script is as follows:

eval $(ssh-agent) > /dev/null
# add the ssh key
ssh-add /root/.ssh/id_rsa

kill $SSH_AGENT_PID

git clone ssh://git@bitbucket.org/project/repo.git 

but when docker is running it gives an error

Cloning into 'repo'...
Host key verification failed.

fatal: Could not read from remote repository.

When I test on my local machine, I can clone the repo without a failure, so I know there is nothing wrong with my ssh keys.

1 Answers

The problem is when you have a setup like this on your local machine you have an access to the terminal for SSH adding the key to the known_hosts asking

The authenticity of host 'server-name (***)' can't be established.
RSA key fingerprint is XXXXXXX.
Are you sure you want to continue connecting (yes/no)?

and you can basically interact, and type yes and ssh-agent add the connection to the known_hosts for you. However, in this case the things happen inside a docker container, and you basically cannot interect with this prompt. The solution for this is to add StrictHostKeyChecking no flag to ssh config, there are several ways to do it for git command, and you can check them here.

So basically, the following is the elegant way to solve this, just make .ssh/config file and add the ssh options we want.

eval $(ssh-agent) > /dev/null
# add the ssh key
ssh-add /root/.ssh/id_rsa

kill $SSH_AGENT_PID

echo "Host bitbucket.org" > /root/.ssh/config
echo "User git" >> /root/.ssh/config
echo "IdentityFile /root/.ssh/id_rsa" >> /root/.ssh/config
echo "StrictHostKeyChecking no" >> /root/.ssh/config

git clone ssh://git@bitbucket.org/project/repo.git 

StrictHostKeyChecking no option just discards the prompt and directly adds the connection to the known_hosts, and basically afterwards can git clone.

Related