Skip to content

Commit

Permalink
Add check for default docker desktop endpoint on linux (#725)
Browse files Browse the repository at this point in the history
According to [this
documentation](https://docs.docker.com/desktop/faqs/linuxfaqs/#context),
the default endpoint for docker desktop on linux is
`unix:///$HOME/.docker/desktop/docker.sock`. Right now, we are only
checking the default location on MacOS,
`unix:///$HOME/.docker/run/docker.sock`. This PR adds a check for this
if the other checks for a default system-wide host and MacOS user space
host fail.

Fixes [#720](#720)

---------

Co-authored-by: Thomas Kappler <[email protected]>
  • Loading branch information
FPVian and thomas11 authored Sep 29, 2023
1 parent 8a90726 commit 9edb709
Showing 1 changed file with 30 additions and 10 deletions.
40 changes: 30 additions & 10 deletions provider/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -777,24 +777,44 @@ func configureDockerClient(configs map[string]string, verify bool) (*client.Clie
return cli, err
}

// Check if the connection works. If not and we used the default host, try the user host.
// Check if the connection works. If not and we used the default host, try the possible user hosts.
// See "Adminless install on macOS" on https://www.docker.com/blog/docker-desktop-4-18/
log.Printf("checking connection to docker daemon at %s", host)
_, err = cli.Ping(context.Background())
if err != nil && !isExplicitHost && runtime.GOOS != "windows" {
testConnection := func(cli *client.Client) bool {
_, err = cli.Ping(context.Background())
if err != nil {
log.Printf("error connecting to docker daemon at %s: %v", cli.DaemonHost(), err)
return false
} else {

Check failure on line 787 in provider/image.go

View workflow job for this annotation

GitHub Actions / lint / lint

`if` block ends with a `return` statement, so drop this `else` and outdent its block (golint)
log.Printf("successful connection to docker daemon at %s", cli.DaemonHost())
return true
}
}

success := testConnection(cli)
if !success && !isExplicitHost && runtime.GOOS != "windows" {
home, err2 := os.UserHomeDir()
if err2 != nil {
return nil, err2
}
userSock := fmt.Sprintf("unix://%s/.docker/run/docker.sock", home)
log.Printf("no connection to docker daemon at %s, trying %s (%v)", host, userSock, err)
cli, err = configureDockerClientInner(configs, userSock)
if err != nil {
log.Printf("no connection to docker daemon at %s, stopping (%v)", host, err)

userHosts := []string{"unix://%s/.docker/run/docker.sock", "unix://%s/.docker/desktop/docker.sock"}
for _, userHost := range userHosts {
userSock := fmt.Sprintf(userHost, home)
cli, err = configureDockerClientInner(configs, userSock)
if err != nil {
return cli, err
}
if testConnection(cli) {
success = true
break
}
}
}

return cli, err
if success {
return cli, err
}
return nil, fmt.Errorf("failed to connect to any docker daemon")
}

func configureDockerClientInner(configs map[string]string, host string) (*client.Client, error) {
Expand Down

0 comments on commit 9edb709

Please sign in to comment.