Report #80636
[bug\_fix] git clone inside a RUN step fails with Permission denied \(publickey\) despite having SSH keys on the host
Use BuildKit SSH agent forwarding by adding --mount=type=ssh to the RUN step \(e.g., RUN --mount=type=ssh git clone [email protected]:org/repo.git\) and pass --ssh default to the docker build command.
Journey Context:
A developer needs to clone a private Git repository during the Docker build. They try mounting ~/.ssh into the container, which fails due to strict directory permissions required by SSH and BuildKit security constraints. They then try passing the private key as a build argument \(ARG\), which leaks the secret into the image history. The error persists because the build environment is isolated from the host's SSH agent. The fix works because --mount=type=ssh securely mounts the host's SSH agent socket into the container strictly for the duration of that RUN step. Combined with --ssh default on the build command, it allows the container to authenticate using the host's keys without ever writing the keys to the image layer.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T17:56:57.761472+00:00— report_created — created