SSH Authentication is a common authentication pattern for using git repos, and it has a lot benefits from a security perspective. The ability to leverage those keys is a common pattern for a lot of the development community out there.
If you are wondering how to setup SSH authentication to Github and/or GHES, you can find steps here.
Now the problem comes from trying to access your local ssh keys from inside the devcontainer. Devcontainers provide an option for supporting this through mounts, and enabling the ssh-agent within the container. The key elements are the following:
"mounts": [
"source=${env:HOME}/.gitconfig,target=/root/.gitconfig,type=bind,consistency=cached",
"source=${env:SSH_AUTH_SOCK},target=/ssh-agent,type=bind"
],
And the following for remoteEnv:
"remoteEnv": {
"SSH_AUTH_SOCK": "/ssh-agent"
},
And the following for a postCreate command:
"postCreateCommand": "git config --global --add safe.directory /workspaces/${localWorkspaceFolderBasename}",
Below is a full devcontainer.json that has azure cli and mounts your github authentication key.
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
// README at: https://github.com/devcontainers/templates/tree/main/src/ubuntu
{
"name": "Ubuntu",
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
"image": "mcr.microsoft.com/devcontainers/base:jammy",
"settings": {
"terminal.integrated.shell.linux": "/bin/bash"
},
"extensions": [
"ms-azuretools.vscode-docker",
"ms-vscode-remote.remote-containers"
],
"mounts": [
"source=${env:HOME}/.gitconfig,target=/root/.gitconfig,type=bind,consistency=cached",
"source=${env:SSH_AUTH_SOCK},target=/ssh-agent,type=bind"
],
"remoteEnv": {
"SSH_AUTH_SOCK": "/ssh-agent"
},
"forwardPorts": [],
"postCreateCommand": "git config --global --add safe.directory /workspaces/${localWorkspaceFolderBasename}",
"features": {
"ghcr.io/devcontainers/features/azure-cli:1": {}
}
}