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 [email protected]: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 .
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 .
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=sshRUN lines, not CMDSo that would work with a RUN, during docker build.
That might not work at runtime (docker run) with a CMD
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With