Search code examples
gitssh-keysdocker-image

How to securely git clone/pip install a private repository into my docker image?


I have a private repo that contains packages I want to pip install. I've spent quite a bit of time reading over various forums and articles about different ways to securely do this. There doesn't seem to be a a consensus on how to best do this (if at all possible). I obviously don't want to expose any ssh keys/secrets in my dockerfile -- I want to be careful about making them available via docker history.


Solution

  • As explained in "Securely build small python docker image from private git repos", you would need to use, with Docker 18.09+

    • --ssh
      You can use the --ssh flag to forward your existing SSH agent key to the builder. Instead of transferring the key data, docker will just notify the builder that such capability is available.
      Now when the builder needs access to a remote server through SSH, it will dial back to the client and ask it to sign the specific request needed for this connection.
      The key itself never leaves the client, and as soon as the command that requested the access has completed there is no information on the builder side to reestablish that remote connection later.

    • Secrets:
      Provides a mount option during the build at /var/run/secrets available only for the command that used it and is not included in the created layer.

    That is:

    docker build --ssh github_ssh_key=/path/to/.ssh/git_ssh_id_rsa .
    

    only the agent connection is shared with that command, and not the actual content of the private key.
    no other commands/steps in the Dockerfile will have access to it.

    The Dockerfile, in a multistage first step, would give a key name github_ssh_key so we can use it when we invoke docker build:

    RUN --mount=type=ssh,id=github_ssh_key pip wheel \
        --no-cache \
        --requirement requirements.txt \
    --wheel-dir=/app/wheels
    

    The OP Jesus Garcia did report (in the comments) making it work:

    I had to use 2 separate RUN commands.

    I'm not sure if it's a limitation of this new feature, or the way I was trying to string together multiple commands in my RUN but I kept getting a publickey permission denied error when I added it as other commands && /bin/sh -c "mount=type=ssh,id=github_ssh_key pip install private-repo" vs RUN --mount=type=ssh,id=github_ssh_key pip install private-repo && more commands ...