Search code examples
dockersshssh-agent

Start ssh-agent on headless docker host


On my desktop machine I've developed a docker container which clones some Git repository. To provide the credentials I mount my local ssh-agent as it is specified in $SSH_AUTH_SOCK into the docker container.

Now I want to deploy this docker image on a headless docker host which is running ubuntu. I've created a key-pair for the server which I've also granted access to the Git repository. But when I login the $SSH_AUTH_SOCK variable is not set and no ssh-agent is running. Just forwarding it via ssh -A is definitely not what I want.

How do I start the ssh-agent properly to make it accessible to the docker container?


Solution

  • In general it is as easy as executing eval $(ssh-agent) > /dev/null in your shell. But this would start a new agent every time as explained by Jon Cairns in the blogpost Understanding ssh-agent and ssh-add.

    To just start it once and get the environment every time use ssh-find-agent.

    Clone the repo (e.g. to ~/ssh-find-agent) and add the following lines to your .bashrc or .zshrc

    . $HOME/ssh-find-agent/ssh-find-agent.sh
    ssh_find_agent -a || eval $(ssh-agent) > /dev/null
    

    This will start the ssh-agent if non is running and set the environment as necessary if an agent is running already.

    Now the $SSH_AUTH_SOCK can be mounted to your docker container.