Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli/connhelper: support overriding the docker binary over SSH #5627

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 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 @@ -47,9 +54,10 @@ func getConnectionHelper(daemonURL string, sshFlags []string) (*ConnectionHelper
}
sshFlags = addSSHTimeout(sshFlags)
sshFlags = disablePseudoTerminalAllocation(sshFlags)
remoteDockerBinary := dockerSSHRemoteBinary()
return &ConnectionHelper{
Dialer: func(ctx context.Context, network, addr string) (net.Conn, error) {
args := []string{"docker"}
args := []string{remoteDockerBinary}
if sp.Path != "" {
args = append(args, "--host", "unix://"+sp.Path)
}
Expand Down Expand Up @@ -91,3 +99,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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would probably be better if this was a per-context setting rather than a global env-variable.
However, it's a niche use-case, so I guess it's okay as an escape hatch and not really a blocker.

@thaJeztah @laurazard WDYT?

if value == "" {
return "docker"
}

return value
}
27 changes: 27 additions & 0 deletions cli/connhelper/connhelper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,30 @@ 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()
assert.Check(t, is.Equal(result, tc.expected))
})
}
}