Skip to content

Instantly share code, notes, and snippets.

@jrforrest
Created November 25, 2015 19:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jrforrest/c31a557620855efe1606 to your computer and use it in GitHub Desktop.
Save jrforrest/c31a557620855efe1606 to your computer and use it in GitHub Desktop.
Docker remote
#!/bin/sh
# Connects to remote docker instances via ssh
# USAGE:
# CONNECT: docker-remote connect user@ssh-server
# DISCONNECT: docker-remote disconnect
# RUN COMMAND: docker-remote <docker cmd>
set -m #shell must support job control
SOCK_PATH=/tmp/docker_remote.sock
LOGFILE_PATH=/tmp/docker_remote.log
PID_PATH=/tmp/docker_remote.pid
if [ "$1" = "connect" ]; then
server=$2
if [ -z $server ]; then
echo "Server must be specified" >&2
exit 1
fi
if [ -f "$PID_PATH" ]; then
echo "Server appears to already be running! \`docker-remote stop\` first."
fi
nohup socat "UNIX-LISTEN:$SOCK_PATH,reuseaddr,fork" \
EXEC:"ssh $server socat STDIO UNIX-CONNECT\:/var/run/docker.sock" \
1>$LOGFILE_PATH 2>$LOGFILE_PATH &
echo $! > $PID_PATH
elif [ "$1" = "disconnect" ]; then
pid=`cat $PID_PATH`
if [ -z $pid ]; then
echo "No pid found in pidfile: $PID_PATH" &>2
exit 1
fi
kill `cat $PID_PATH`
rm $PID_PATH
rm $SOCK_PATH
else
docker -H unix://$SOCK_PATH $@
fi
@jrforrest
Copy link
Author

This just forwards docker commands to a local socket via ssh connections to a remote host. It is not very quick, and the script is brittle, but it does in a pinch when you want to do remote docker builds without setting up SSL.

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