Skip to content

Commit 43dbdcf

Browse files
committed
cli/connhelper: support overriding the docker binary over SSH
This change adds the ability to override the docker binary used when executing commands over SSH. This is useful when the docker binary is not in the PATH of the SSH user, or when the binary is not named `docker`. If `DOCKER_SSH_REMOTE_BINARY` is set in the environment, the value will be used as the docker binary when executing commands over SSH. If the environment variable is not set, the default value of `docker` will be used. Signed-off-by: Matthew MacLeod <[email protected]>
1 parent 9861ce9 commit 43dbdcf

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

cli/connhelper/connhelper.go

+20-1
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,20 @@ import (
55
"context"
66
"net"
77
"net/url"
8+
"os"
89
"strings"
910

1011
"github.com/docker/cli/cli/connhelper/commandconn"
1112
"github.com/docker/cli/cli/connhelper/ssh"
1213
"github.com/pkg/errors"
1314
)
1415

16+
const (
17+
// DockerSSHRemoteBinaryEnv is the environment variable that can be used to
18+
// override the default Docker binary called over SSH
19+
DockerSSHRemoteBinaryEnv = "DOCKER_SSH_REMOTE_BINARY"
20+
)
21+
1522
// ConnectionHelper allows to connect to a remote host with custom stream provider binary.
1623
type ConnectionHelper struct {
1724
Dialer func(ctx context.Context, network, addr string) (net.Conn, error)
@@ -49,7 +56,7 @@ func getConnectionHelper(daemonURL string, sshFlags []string) (*ConnectionHelper
4956
sshFlags = disablePseudoTerminalAllocation(sshFlags)
5057
return &ConnectionHelper{
5158
Dialer: func(ctx context.Context, network, addr string) (net.Conn, error) {
52-
args := []string{"docker"}
59+
args := []string{dockerSSHRemoteBinary()}
5360
if sp.Path != "" {
5461
args = append(args, "--host", "unix://"+sp.Path)
5562
}
@@ -91,3 +98,15 @@ func disablePseudoTerminalAllocation(sshFlags []string) []string {
9198
}
9299
return append(sshFlags, "-T")
93100
}
101+
102+
// dockerSSHRemoteBinary returns the binary to use when executing Docker
103+
// commands over SSH. It defaults to "docker" if the DOCKER_SSH_REMOTE_BINARY
104+
// environment variable is not set.
105+
func dockerSSHRemoteBinary() string {
106+
value := os.Getenv(DockerSSHRemoteBinaryEnv)
107+
if value == "" {
108+
return "docker"
109+
}
110+
111+
return value
112+
}

cli/connhelper/connhelper_test.go

+29
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,32 @@ func TestDisablePseudoTerminalAllocation(t *testing.T) {
6363
})
6464
}
6565
}
66+
67+
func TestDockerSSHBinaryOverride(t *testing.T) {
68+
testCases := []struct {
69+
name string
70+
env string
71+
expected string
72+
}{
73+
{
74+
name: "Default",
75+
env: "",
76+
expected: "docker",
77+
},
78+
{
79+
name: "Override",
80+
env: "other-binary",
81+
expected: "other-binary",
82+
},
83+
}
84+
85+
for _, tc := range testCases {
86+
t.Run(tc.name, func(t *testing.T) {
87+
t.Setenv(DockerSSHRemoteBinaryEnv, tc.env)
88+
result := dockerSSHRemoteBinary()
89+
if result != tc.expected {
90+
t.Errorf("expected %q, got %q", tc.expected, result)
91+
}
92+
})
93+
}
94+
}

0 commit comments

Comments
 (0)