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.
This commit is contained in:
Charlie Moog 2021-12-27 23:58:42 -06:00
parent 892fc090b6
commit 0bd4168dc9
No known key found for this signature in database
GPG key ID: 54C2F30EA784F821

View file

@ -73,9 +73,12 @@ func (c *DockerCommand) NewCommandObject(obj CommandObject) CommandObject {
return defaultObj 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() ctx := context.Background()
u, err := url.Parse(os.Getenv("DOCKER_HOST")) u, err := url.Parse(os.Getenv(key))
if err != nil { if err != nil {
// if no or an invalid docker host is specified, continue nominally // if no or an invalid docker host is specified, continue nominally
return nil return nil
@ -87,7 +90,12 @@ func handleSSHHosts(c *client.Client) error {
if err != nil { if err != nil {
return fmt.Errorf("tunnel ssh docker host: %w", err) 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 return nil
} }
@ -179,11 +187,15 @@ func tunnelSSH(ctx context.Context, host, localSocket string) error {
// //
// Handle special cases including `ssh://` host schemes. // Handle special cases including `ssh://` host schemes.
func clientBuilder(c *client.Client) error { func clientBuilder(c *client.Client) error {
err := client.FromEnv(c) err := handleSSHDockerHost()
if err != nil { if err != nil {
return err return err
} }
return handleSSHHosts(c) err = client.FromEnv(c)
if err != nil {
return err
}
return nil
} }
// NewDockerCommand it runs docker commands // NewDockerCommand it runs docker commands