From 0bd4168dc9fbb21cf27f4275d924af4a315a43c9 Mon Sep 17 00:00:00 2001 From: Charlie Moog Date: Mon, 27 Dec 2021 23:58:42 -0600 Subject: [PATCH] override DOCKER_HOST environment variable directly Rather than only overriding the Go docker client configuration, we now override the process DOCKER_HOST so as to properly configure docker-cli child processes. --- pkg/commands/docker.go | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/pkg/commands/docker.go b/pkg/commands/docker.go index 4045852d..9a7a8f22 100644 --- a/pkg/commands/docker.go +++ b/pkg/commands/docker.go @@ -73,9 +73,12 @@ func (c *DockerCommand) NewCommandObject(obj CommandObject) CommandObject { return defaultObj } -func handleSSHHosts(c *client.Client) error { +// handleSSHDockerHost overrides the DOCKER_HOST environment variable +// to point towards a local unix socket tunneled over SSH to the specified ssh host. +func handleSSHDockerHost() error { + const key = "DOCKER_HOST" ctx := context.Background() - u, err := url.Parse(os.Getenv("DOCKER_HOST")) + u, err := url.Parse(os.Getenv(key)) if err != nil { // if no or an invalid docker host is specified, continue nominally return nil @@ -87,7 +90,12 @@ func handleSSHHosts(c *client.Client) error { if err != nil { return fmt.Errorf("tunnel ssh docker host: %w", err) } - return client.WithHost(newDockerHost)(c) + err = os.Setenv(key, newDockerHost) + if err != nil { + return fmt.Errorf("override DOCKER_HOST to tunneled socket: %w", err) + } + + return nil } return nil } @@ -179,11 +187,15 @@ func tunnelSSH(ctx context.Context, host, localSocket string) error { // // Handle special cases including `ssh://` host schemes. func clientBuilder(c *client.Client) error { - err := client.FromEnv(c) + err := handleSSHDockerHost() if err != nil { return err } - return handleSSHHosts(c) + err = client.FromEnv(c) + if err != nil { + return err + } + return nil } // NewDockerCommand it runs docker commands