From b5f3d1f5ed4b9fedb65f72d536f6180752cb5b10 Mon Sep 17 00:00:00 2001 From: Tugdual Saunier Date: Sat, 5 Oct 2024 15:03:39 +0200 Subject: [PATCH 1/2] remove custom timeout for docker builtin --- envs/docker.go | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/envs/docker.go b/envs/docker.go index 2c3fc649..fb99494d 100644 --- a/envs/docker.go +++ b/envs/docker.go @@ -23,7 +23,6 @@ import ( "bytes" "context" "fmt" - "net" "net/url" "os" "path/filepath" @@ -69,17 +68,7 @@ func (l *Local) RelationshipsFromDocker() Relationships { return nil } - opts := [](docker.Opt){docker.FromEnv} - if host := os.Getenv(docker.EnvOverrideHost); host != "" && !strings.HasPrefix(host, "unix://") { - // Setting a dialer on top of a unix socket breaks the connection - // as the client then tries to connect to http:///path/to/socket and - // thus tries to resolve the /path/to/socket host - dialer := &net.Dialer{ - Timeout: 2 * time.Second, - } - opts = append(opts, docker.WithDialContext(dialer.DialContext)) - } - client, err := docker.NewClientWithOpts(opts...) + client, err := docker.NewClientWithOpts(docker.WithTimeout(2*time.Second), docker.FromEnv) if err != nil { if l.Debug { fmt.Fprintf(os.Stderr, "ERROR: %s\n", err) From 646dabed6f536a6273ab7fec9421f4d468732a4a Mon Sep 17 00:00:00 2001 From: Tugdual Saunier Date: Sat, 5 Oct 2024 15:06:54 +0200 Subject: [PATCH 2/2] Make Docker connection use Docker Desktop socket if available --- envs/docker.go | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/envs/docker.go b/envs/docker.go index fb99494d..dcb8df2d 100644 --- a/envs/docker.go +++ b/envs/docker.go @@ -68,7 +68,7 @@ func (l *Local) RelationshipsFromDocker() Relationships { return nil } - client, err := docker.NewClientWithOpts(docker.WithTimeout(2*time.Second), docker.FromEnv) + client, err := docker.NewClientWithOpts(docker.WithTimeout(2*time.Second), docker.FromEnv, dockerUseDesktopSocketIfAvailable) if err != nil { if l.Debug { fmt.Fprintf(os.Stderr, "ERROR: %s\n", err) @@ -462,6 +462,24 @@ func formatDockerPort(port uint16) string { return strconv.FormatInt(int64(port), 10) } +func dockerUseDesktopSocketIfAvailable(c *docker.Client) error { + if c.DaemonHost() != docker.DefaultDockerHost { + return nil + } + + homeDir, err := os.UserHomeDir() + if err != nil { + return err + } + + socketPath := filepath.Join(homeDir, ".docker/run/docker.sock") + if _, err := os.Stat(socketPath); err != nil { + return nil + } + + return docker.WithHost(`unix://` + socketPath)(c) +} + func getEnvValue(env string, key string) string { if len(key) == len(env) { return ""