Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sharing ssh credentials with dev container in vscode

I have setup a VS Code dev container and I can run and debug my code inside. My ~/.gitconfig from my host system was correctly copied into the container. However, my ssh credentials don't seem to work (or are not available), neither are my gnupg signing files (~/.gnupg) available.

Must I manually copy these files ( ~/.ssh and ~/.gnupg) into the container?

The documentation cautions:

There are some cases when you may be cloning your repository using SSH keys instead of a credential helper. To enable this scenario, the extension will automatically forward your local SSH agent if one is running.

The only stated requirement is the SSH-agent server running on my host system, which it is:

C:\Windows\system32> Get-Service ssh-agent

Status   Name               DisplayName
------   ----               -----------
Running  ssh-agent          OpenSSH Authentication Agent

What is the correct way to make my SSH credentials available to my dev container?

like image 204
Daniel Stephens Avatar asked Mar 21 '26 09:03

Daniel Stephens


2 Answers

It is also possible to achieve this with the mount capability of the devcontainer.json configuration file:

    {
        "name": "container name",
        ...
        "remoteUser": "root",  
        "mounts": [
            "source=${localEnv:HOME}/.ssh,target=/root/.ssh,type=bind,consistency=cached",
        ]
       ...
    }
like image 189
Kanekotic Avatar answered Mar 23 '26 03:03

Kanekotic


With my version of dev containers there is a folder

/home/vscode

The correct mount option is therefore

"mounts": [
    "source=${localEnv:HOME}/.ssh,target=/home/vscode/.ssh,type=bind,consistency=cached",
]

extra info: I needed this, since I have two github accounts with two ssh keys. When only the ssh agent is forwarded, git is picking up the wrong ssh key and authentication fails.

To solve this I added two hosts in the .ssh/config.

Host git_priv
  Hostname github.com
  IdentityFile ~/.ssh/id_rsa_git_private
  IdentitiesOnly yes

Host github.com
  Hostname github.com
  IdentityFile ~/.ssh/id_rsa
  IdentitiesOnly yes

Basically I set one ssh key as standard (using github.com as the host) and the the other to only work with github-private.com as domain. If I now inlclude this git config with the above command into the devcontainer, I get the correct ssh key resolution.

like image 42
jo_rob Avatar answered Mar 23 '26 05:03

jo_rob



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!