gitdockersshdockerfiledocker-buildkit

How to use the mounted ssh in docker for subsequent commands in Dockerfile


I have to do a git push while running my container using CMD. But the ssh is not available in the last line i.e in the CMD part, to do a git push. What can I do here to get the ssh key for git push? Someone please help me.

Please find my Dockerfile

# syntax = docker/dockerfile:1.0-experimental
FROM continuumio/anaconda3

# Install git
RUN apt-get update && apt-get install -y git

# Download public key for github.com
RUN mkdir -p -m 0600 ~/.ssh && ssh-keyscan github.com >> ~/.ssh/known_hosts

# Clone private repository
RUN --mount=type=ssh git clone git@github.com:myorg/myproject.git myproject

CMD python myproject/src/example.py && git push

Then I use the following command to pass my ssh key during run

docker build --ssh default .

Solution

  • As seen in "BuildKit / Using SSH to access private data in builds" and "Build secrets and SSH forwarding in Docker 18.09", I see that used with:

    So that would work with a RUN, during docker build.
    That might not work at runtime (docker run) with a CMD