From 42b7ba18ea4346b78680ebaaba421c82cd9b617a Mon Sep 17 00:00:00 2001 From: Rajiv Kushwaha Date: Wed, 14 Feb 2024 14:46:56 +0530 Subject: [PATCH] fix: Properly support remote docker daemon over ssh --- pkg/commands/docker.go | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/pkg/commands/docker.go b/pkg/commands/docker.go index e96c96533..287ee6cea 100644 --- a/pkg/commands/docker.go +++ b/pkg/commands/docker.go @@ -27,7 +27,8 @@ import ( ) const ( - APIVersion = "1.25" + APIVersion = "1.25" + dockerHostEnvKey = "DOCKER_HOST" ) // DockerCommand is our main docker interface @@ -71,14 +72,28 @@ func (c *DockerCommand) NewCommandObject(obj CommandObject) CommandObject { // NewDockerCommand it runs docker commands func NewDockerCommand(log *logrus.Entry, osCommand *OSCommand, tr *i18n.TranslationSet, config *config.AppConfig, errorChan chan error) (*DockerCommand, error) { + dockerHost, err := determineDockerHost() + if err != nil { + ogLog.Printf("> could not determine host %v", err) + } + + // NOTE: Inject the determined docker host to the environment. This allows the + // `SSHHandler.HandleSSHDockerHost()` to create a local unix socket tunneled + // over SSH to the specified ssh host. + if strings.HasPrefix(dockerHost, "ssh://") { + os.Setenv(dockerHostEnvKey, dockerHost) + } + tunnelCloser, err := ssh.NewSSHHandler(osCommand).HandleSSHDockerHost() if err != nil { ogLog.Fatal(err) } - dockerHost, err := determineDockerHost() - if err != nil { - ogLog.Printf("> could not determine host %v", err) + // Retrieve the docker host from the environment which could have been set by + // the `SSHHandler.HandleSSHDockerHost()` and override `dockerHost`. + dockerHostFromEnv := os.Getenv(dockerHostEnvKey) + if dockerHostFromEnv != "" { + dockerHost = dockerHostFromEnv } cli, err := client.NewClientWithOpts(client.FromEnv, client.WithVersion(APIVersion), client.WithHost(dockerHost))