SSH connection in Docker image
Had a use case to checkout a git repository (from Github) via ssh in a Docker image. Turns out that it isn't simple and I'm not entirely sure what's the reason. This entry is a sorrow lesson without a fix. Help is welcome.
In any case, I was expecting this to work but it did not:
FROM ubuntu:latest RUN apt-get update && apt-get install -y \ git \ openssh-client RUN mkdir -p /root/.ssh && \ chmod 0700 /root/.ssh && \ ssh-keyscan github.com > /root/.ssh/known_hosts RUN ssh-keygen -t rsa -b 4096 -f /root/.ssh/id_rsa -N "" && chmod 600 /root/.ssh/id_rsa RUN echo "Host github.com\n\tStrictHostKeyChecking no\n" >> /root/.ssh/config RUN ssh-agent bash -c 'ssh-add /root/.ssh/id_rsa && firstname.lastname@example.org:laszukdawid/tiny-repo.git'
I'm getting the following error:
> [6/6] RUN ssh-agent bash -c 'ssh-add /root/.ssh/id_rsa && git clone email@example.com:laszukdawid/tiny-repo.git': 0.416 Identity added: /root/.ssh/id_rsa (root@buildkitsandbox) 0.421 Cloning into 'tiny-repo'... 1.173 firstname.lastname@example.org: Permission denied (publickey). 1.175 fatal: Could not read from remote repository. 1.175 1.175 Please make sure you have the correct access rights 1.175 and the repository exists.
Conclusion, for now, is to use https instead of ssh.