Skip to content

Commit

Permalink
cli/connhelper: support overriding the docker binary over SSH
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mattmacleod committed Nov 16, 2024
1 parent 9861ce9 commit 951dcee
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
21 changes: 20 additions & 1 deletion cli/connhelper/connhelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,20 @@ import (
"context"
"net"
"net/url"
"os"
"strings"

"github.com/docker/cli/cli/connhelper/commandconn"
"github.com/docker/cli/cli/connhelper/ssh"
"github.com/pkg/errors"
)

const (
// DockerSSHRemoteBinaryEnv is the environment variable that can be used to
// override the default Docker binary called over SSH
DockerSSHRemoteBinaryEnv = "DOCKER_SSH_REMOTE_BINARY"
)

// ConnectionHelper allows to connect to a remote host with custom stream provider binary.
type ConnectionHelper struct {
Dialer func(ctx context.Context, network, addr string) (net.Conn, error)
Expand Down Expand Up @@ -49,7 +56,7 @@ func getConnectionHelper(daemonURL string, sshFlags []string) (*ConnectionHelper
sshFlags = disablePseudoTerminalAllocation(sshFlags)
return &ConnectionHelper{
Dialer: func(ctx context.Context, network, addr string) (net.Conn, error) {
args := []string{"docker"}
args := []string{dockerSSHRemoteBinary()}
if sp.Path != "" {
args = append(args, "--host", "unix://"+sp.Path)
}
Expand Down Expand Up @@ -91,3 +98,15 @@ func disablePseudoTerminalAllocation(sshFlags []string) []string {
}
return append(sshFlags, "-T")
}

// dockerSSHRemoteBinary returns the binary to use when executing Docker
// commands over SSH. It defaults to "docker" if the DOCKER_SSH_REMOTE_BINARY
// environment variable is not set.
func dockerSSHRemoteBinary() string {
value := os.Getenv(DockerSSHRemoteBinaryEnv)
if value == "" {
return "docker"
}

return value
}
29 changes: 29 additions & 0 deletions cli/connhelper/connhelper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,32 @@ func TestDisablePseudoTerminalAllocation(t *testing.T) {
})
}
}

func TestDockerSSHBinaryOverride(t *testing.T) {
testCases := []struct {
name string
env string
expected string
}{
{
name: "Default",
env: "",
expected: "docker",
},
{
name: "Override",
env: "other-binary",
expected: "other-binary",
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
t.Setenv(DockerSSHRemoteBinaryEnv, tc.env)
result := dockerSSHRemoteBinary()
if result != tc.expected {
t.Errorf("expected %q, got %q", tc.expected, result)
}
})
}
}

0 comments on commit 951dcee

Please sign in to comment.