Skip to content

Instantly share code, notes, and snippets.

@rcoup
Created July 9, 2015 13:28
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rcoup/53e8dee9f5ea27a51855 to your computer and use it in GitHub Desktop.
Save rcoup/53e8dee9f5ea27a51855 to your computer and use it in GitHub Desktop.
SSH Agent Forwarding for boot2docker
#!/bin/bash
# "docker run" with SSH Agent Forwarding for boot2docker
# QuickStart:
# 1. Download to ~/bin/docker-run-ssh and chmod +x it
# 2. docker-run-ssh [normal args to docker run...]
# Use a unique ssh socket name per-invocation of this script
SSH_SOCK=boot2docker.$$.ssh.socket
# ssh into boot2docker with agent forwarding
ssh -i ~/.ssh/id_boot2docker \
-o StrictHostKeyChecking=no \
-o IdentitiesOnly=yes \
-o UserKnownHostsFile=/dev/null \
-o LogLevel=quiet \
-p 2022 docker@localhost \
-A -M -S $SSH_SOCK -f -n \
tail -f /dev/null
# get the agent socket path from the boot2docker vm
B2D_AGENT_SOCK=$(ssh -S $SSH_SOCK docker@localhost echo \$SSH_AUTH_SOCK)
# mount the socket (from the boot2docker vm) onto the docker container
# and set the ssh agent environment variable so ssh tools pick it up
docker run \
-v $B2D_AGENT_SOCK:/ssh-agent \
-e "SSH_AUTH_SOCK=/ssh-agent" \
"$@"
# we're done; kill off the boot2docker ssh agent
ssh -S $SSH_SOCK -O exit docker@localhost
@shaunc
Copy link

shaunc commented Jan 13, 2017

Doesn't work with docker for mac, unfortunately ssh -S $SSH_SOCK docker@localhost echo \$SSH_AUTH_SOCK asks for a password(?)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment