Skip to content

Instantly share code, notes, and snippets.

@vilhalmer
Last active April 9, 2018 02:09
Show Gist options
  • Save vilhalmer/3db7806bb479bf594d6ee124c5f3d6a4 to your computer and use it in GitHub Desktop.
Save vilhalmer/3db7806bb479bf594d6ee124c5f3d6a4 to your computer and use it in GitHub Desktop.
docker-forward
#!/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
@vilhalmer
Copy link
Author

Updated it to leave existing sessions alone so you can eval it in multiple shells.

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