Last active
February 10, 2020 11:23
-
-
Save bcoughlan/97d9ad5ce6f43461d301b5312e55e0d6 to your computer and use it in GitHub Desktop.
SSH forward a remote Docker daemon in a bash script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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/