Git + SSH key issues on Windows with VSCode + WSL2 + Docker

Viewed 803

I've googled and experimented with this a lot over the last few weeks and can't seem to hit the right combo to get this working.

I'm on Windows 10, and am using VSCode with Docker and WSL2. For the life of me I can't get git + ssh keys working so that I can push and pull from within my Docker containers.

Using the Windows Terminal app and Ubuntu 20.04 on WSL2 works fine. I copied my SSH keys over from the Windows side of things and changed the permissions appropriately and have no issues there.

In my project using Docker Compose, I've tried using a volume (~/.ssh:/root/.ssh), but the issue with this configuration is the owner of everything in the .ssh directory becomes 1000:1000 and when I try to do any git operations, I get a message:

bad permissions or owner on /root/.ssh/config

I've also tried not having that volume and using the ssh agent forwarding as outlined in the VSCode documentation, but when I try that I get permission denied from github when I try to pull or push.

Does anyone have any luck getting this working? It isn't a huge deal to do all my git commands from a terminal, but it'd sure be convenient to be able to do it successfully from within the Docker container.

1 Answers

/root/.ssh/config means your container would need to run with USER root, instead of a default user 1000:1000.

I would advise, if possible, to:

  • and if possible to build your own image where you define the user ID/group to match your local user id/group.
  • mount your ssh folder to a container ~user/.ssh path

That way, the internal id/group of the container would match the external host user id/group, and the SSH files would be readable from inside the container.

Related