Fixing Git Pull in Docker: SSH Key and Dubious

Posted on May 18, 2025 by admin 1 min

When using Docker to run Git commands inside a container, such as git pull, you might encounter errors like:

  • fatal: detected dubious ownership in repository
  • Host key verification failed. Could not read from remote repository.

These problems usually stem from two issues:

  1. Ownership mismatch: Docker containers often run as root, and when you mount your current directory into the container, Git sees that the repository is owned by a different user.
  2. Missing SSH keys: The container doesn’t have access to your host's SSH credentials, so it can't authenticate with private repositories.

✅ The Fix

Here’s a Docker one-liner that solves both problems:

```bash docker run --rm \ --entrypoint /bin/sh \ -v "$(pwd)":/workdir \ -v "$HOME/.ssh:/root/.ssh:ro" \ -w /workdir \ alpine/git \ -c "git config --global --add safe.directory /workdir && git pull"

Note that it is possible that the container changes the permission of the file to something else. This means the host user will no longer be able to change it. This command also helps in that situation.