From a5a3b85eb7e53058287a2dbe562ed9fd9a7a9137 Mon Sep 17 00:00:00 2001
From: Adar Nimrod <nimrod@shore.co.il>
Date: Sun, 21 Apr 2019 22:50:46 +0300
Subject: [PATCH] Add docker remote sub-command.

Add the docker remote sub-command (actually the _docker-remote script
but can be invoked as a sub-command with the wrapper script). It creates
a local socket that's forwarded to the dockerd socket on the remote host
via ssh. Requires OpenSSH 6.7 or later (for Unix socket forwarding and
%C token. Typical invocation would be `$(docker remote
host.domain.example)`. A blog post will follow.
---
 Documents/bin/_docker-remote | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)
 create mode 100755 Documents/bin/_docker-remote

diff --git a/Documents/bin/_docker-remote b/Documents/bin/_docker-remote
new file mode 100755
index 0000000..ff289e9
--- /dev/null
+++ b/Documents/bin/_docker-remote
@@ -0,0 +1,24 @@
+#!/bin/sh
+set -eu
+
+[ -n "$1" ] || { echo "You must specify host." >> /dev/stderr; exit 1; }
+host="$1"
+localsocket="$HOME/.ssh/docker_$host.sock"
+ssh="ssh -fnNTS ~/.ssh/%C.sock"
+
+# Check for exiting forward
+if ! $ssh -O check -L "$localsocket:/var/run/docker.sock" "$host" 2> /dev/null
+then
+
+    # Delete socket if it exists
+    [ ! -S "$localsocket" ] || rm "$localsocket"
+
+    # Open SSH ControlMaster
+    $ssh -o ExitOnForwardFailure=no -o ControlMaster=auto -o ControlPersist=0 "$host"
+
+    # Forward socket
+    $ssh -O forward -L "$localsocket:/var/run/docker.sock" "$host"
+fi
+
+# Echo the DOCKER_HOST export command, for eval'ing.
+echo "export DOCKER_HOST=unix://$localsocket"
-- 
GitLab