fix: Properly support remote docker daemon over ssh · jesseduffield/lazydocker@42b7ba1 (original) (raw)

Original file line number Diff line number Diff line change
@@ -27,7 +27,8 @@ import (
27 27 )
28 28
29 29 const (
30 -APIVersion = "1.25"
30 +APIVersion = "1.25"
31 +dockerHostEnvKey = "DOCKER_HOST"
31 32 )
32 33
33 34 // DockerCommand is our main docker interface
@@ -71,14 +72,28 @@ func (c *DockerCommand) NewCommandObject(obj CommandObject) CommandObject {
71 72
72 73 // NewDockerCommand it runs docker commands
73 74 func NewDockerCommand(log *logrus.Entry, osCommand *OSCommand, tr *i18n.TranslationSet, config *config.AppConfig, errorChan chan error) (*DockerCommand, error) {
75 +dockerHost, err := determineDockerHost()
76 +if err != nil {
77 +ogLog.Printf("> could not determine host %v", err)
78 + }
79 +
80 +// NOTE: Inject the determined docker host to the environment. This allows the
81 +// `SSHHandler.HandleSSHDockerHost()` to create a local unix socket tunneled
82 +// over SSH to the specified ssh host.
83 +if strings.HasPrefix(dockerHost, "ssh://") {
84 +os.Setenv(dockerHostEnvKey, dockerHost)
85 + }
86 +
74 87 tunnelCloser, err := ssh.NewSSHHandler(osCommand).HandleSSHDockerHost()
75 88 if err != nil {
76 89 ogLog.Fatal(err)
77 90 }
78 91
79 -dockerHost, err := determineDockerHost()
80 -if err != nil {
81 -ogLog.Printf("> could not determine host %v", err)
92 +// Retrieve the docker host from the environment which could have been set by
93 +// the `SSHHandler.HandleSSHDockerHost()` and override `dockerHost`.
94 +dockerHostFromEnv := os.Getenv(dockerHostEnvKey)
95 +if dockerHostFromEnv != "" {
96 +dockerHost = dockerHostFromEnv
82 97 }
83 98
84 99 cli, err := client.NewClientWithOpts(client.FromEnv, client.WithVersion(APIVersion), client.WithHost(dockerHost))