Last active
April 9, 2018 02:09
-
-
Save vilhalmer/3db7806bb479bf594d6ee124c5f3d6a4 to your computer and use it in GitHub Desktop.
docker-forward
This file contains 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
#!/usr/bin/env zsh | |
# Poor soul's `docker-machine env` equivalent using ssh forwarding. | |
# Assumes that the remote docker is listening on tcp://127.0.0.1:2375. | |
LOCAL_DOCKER_PORT=${LOCAL_DOCKER_PORT:-22375} | |
pidfile="/var/run/user/$(id -u)/docker-forward-session.pid" | |
existing_session=$(cat $pidfile 2>/dev/null) | |
if [[ $1 == stop ]]; then | |
kill -TERM $existing_session | |
rm $pidfile &>/dev/null | |
cat <<EOF | |
export DOCKER_HOST= | |
# (You should eval this command) | |
EOF | |
exit 0 | |
fi | |
if [[ ! "$existing_session" -gt 0 ]]; then | |
ssh -NTL ${LOCAL_DOCKER_PORT}:localhost:2375 $1 &>/dev/null & disown | |
echo $! > $pidfile | |
fi | |
cat <<EOF | |
export DOCKER_HOST=tcp://localhost:$LOCAL_DOCKER_PORT | |
# (You should eval this command) | |
EOF |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated it to leave existing sessions alone so you can eval it in multiple shells.