Sharing SSH Keys With a Devcontainer

VS Code devcontainers are a great resource for creating reusable containers to share between developers on the same project. When properly setup, it automatically passes your SSH credentials to the container. When this is not set up, the git push/pull functionality in VS Code won’t work (you will still be able to make commits in the devcontainer and then push/pull from the CLI you launched Code with).

The way to do this is to use an SSH agent to forward your credentials. On most systems these aren’t started automatically, so for convenience you will probably want to add the start up to your .bash_profile or .bashrc.

I have found the following useful; it includes a short check to make sure you aren’t running multiple ssh-agents in one session:

SSH_ENV="$HOME/.ssh/agent-environment"

function start_agent {
    echo "Initialising new SSH agent..."
    /usr/bin/ssh-agent | sed 's/^echo/#echo/' > "${SSH_ENV}"
    echo succeeded
    chmod 600 "${SSH_ENV}"
    . "${SSH_ENV}" > /dev/null
    /usr/bin/ssh-add;
}

# Source SSH settings, if applicable
if [ -f "${SSH_ENV}" ]; then
    . "${SSH_ENV}" > /dev/null
    ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || {
        start_agent;
    }
else
    start_agent;
fi

You can verify that your keys are working by running ssh-add -l from the VS Code terminal. This should print your host SSH key.

See also the official documentation.

D. Michael Senter
D. Michael Senter
Research Statistician Developer

My research interests include data analytics and missing data.