Skip to content

Instantly share code, notes, and snippets.

@bcoughlan
Last active February 10, 2020 11:23
Show Gist options
  • Save bcoughlan/97d9ad5ce6f43461d301b5312e55e0d6 to your computer and use it in GitHub Desktop.
Save bcoughlan/97d9ad5ce6f43461d301b5312e55e0d6 to your computer and use it in GitHub Desktop.
SSH forward a remote Docker daemon in a bash script
function ssh_forward_docker_daemon {
# Create a temporary file and delete. We just want the filename.
# Needed for the cleanup to identify which SSH process to kill
SSH_SOCKET=$(mktemp)
rm $SSH_SOCKET
# Clean up on exit
trap "exit" INT TERM
trap "ssh -S $SSH_SOCKET -O exit $1" EXIT
# Set up tunnel and return
ssh -S $SSH_SOCKET -f -M -NL localhost:2377:/var/run/docker.sock root@$1
# Configure Docker daemon to point at tunnel
export DOCKER_HOST="tcp://localhost:2377"
}
ssh_forward_docker_daemon $HOST
@bcoughlan
Copy link
Author

Note: The SSH user (root in this example) needs to have permissions to access the Docker daemon. See here for how to set up another user: https://docs.docker.com/install/linux/linux-postinstall/

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