Sub topic 11
Accessing private Git repositories with SSH mounts
This chapter assumes you can write a Dockerfile, run docker build, and use RUN --mount with secret mounts from the previous chapters. You also need a working SSH key and a running ssh-agent on the build host.
The problem
A build sometimes has to clone a private Git repository or install a dependency from one. The SSH key that grants access lives on your machine. Copying it into the image with COPY or passing it through ARG stores it in a layer, even if a later step deletes it. Anyone who can pull the image can recover the key.
An SSH mount solves this by forwarding your SSH agent into a single RUN step. The agent is a process on the host that holds your unlocked keys and signs authentication requests. The build step sees only a socket for talking to that agent. The private key never enters the build container, so it cannot end up in a layer.
How the mount works
When a RUN step uses --mount=type=ssh, BuildKit creates a Unix socket inside the container and sets the SSH_AUTH_SOCK environment variable to point to it. SSH clients such as git and ssh read that variable automatically. The socket exists only while that step runs.
The Dockerfile
# syntax=docker/dockerfile:1
FROM alpine/git AS fetch
RUN mkdir -p -m 0700 ~/.ssh && ssh-keyscan github.com >> ~/.ssh/known_hosts
RUN --mount=type=ssh \
git clone [email protected]:example/private-repo.git /srcThe first RUN creates the ~/.ssh directory and records the server's host key in known_hosts. Without that entry, SSH would stop to ask whether to trust the host, and a build cannot answer prompts. The second RUN clones the repository using the forwarded agent.
Running the build
Load your key into the agent, then ask BuildKit to forward it.
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
docker build --ssh default -t myapp .The value default means "use the agent that SSH_AUTH_SOCK points to on the host". You can also give an id and a path, such as --ssh github=$HOME/.ssh/id_ed25519. In that form, BuildKit reads the key file itself, and the file must not require a passphrase. A Dockerfile can then select that id with --mount=type=ssh,id=github.
If the build fails with a permission error, run ssh-add -l on the host. An empty list means the agent holds no key, so the server rejects the clone.
Things to watch for
ssh-keyscan fetches whatever key the network returns at build time. That makes it convenient but not authenticated. For stricter builds, store the expected host key in your repository and COPY it to known_hosts. Host keys are public information, so this does not leak anything.
Copying a clone out of the fetch stage keeps the result clean. In a multi-stage build, use COPY --from=fetch /src /src in the final stage. The .git directory comes along unless you remove it, and it can include remote URLs and history you may not want to ship.
Comparison with secret mounts
| Aspect | `type=secret` | `type=ssh` |
|---|---|---|
| What the step receives | A file containing the value | A socket to an agent |
| Typical use | Tokens, `.npmrc`, credentials files | Git over SSH |
| Does the step see the key? | Yes, the file contents | No, only signatures |
| Build flag | `--secret` | `--ssh` |
Exercise
A teammate's build prints "Permission denied (publickey)" during the git clone step that uses --mount=type=ssh. The Dockerfile looks correct, and they ran docker build -t myapp .. Name two likely causes and how to check each.
Show answer
First, the build was run without --ssh default, so no agent socket was forwarded. Add the flag, and use required=true on the mount so the build fails early next time. Second, the host agent holds no key, or not the key authorized for that repository. Run ssh-add -l on the host and add the key with ssh-add if the list is empty or wrong.
Exercise
Why is --mount=type=ssh safer than copying ~/.ssh/id_ed25519 into the image and deleting it in a later RUN step?
Show answer
A deleted file still exists in the earlier layer, so anyone with the image can extract the key. With an SSH mount, the key never enters the container. The step receives only an agent socket, which exists for the duration of that one RUN instruction.