9

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 .

2 Answers2

9

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:

  • --mount=type=ssh
  • only on RUN lines, not CMD

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

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
3

Just for posterity, there are 3 prerequisites of this working, so make sure that build is using buildx, inside the Dockerfile you use the RUN command with --mount=type=ssh and you are passing --ssh default parameter to the build command:

export DOCKER_BUILDKIT=1

FROM ...
RUN --mount=type=ssh composer install --no-dev --no-interaction

docker build --ssh default .
Razvan Grigore
  • 1,649
  • 17
  • 17