SSH connection in Docker image

Dawid Laszuk published on
2 min, 204 words

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 && git@github.com: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 git@github.com:laszukdawid/tiny-repo.git':
0.416 Identity added: /root/.ssh/id_rsa (root@buildkitsandbox)
0.421 Cloning into 'tiny-repo'...
1.173 git@github.com: 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.