diff --git a/docker/opts/hosts.go b/docker/opts/hosts.go index ff126e0d..5d47502e 100644 --- a/docker/opts/hosts.go +++ b/docker/opts/hosts.go @@ -7,10 +7,16 @@ import ( "fmt" "net" "net/url" + "os" "strconv" "strings" ) +const ( + dockerSocket = "/var/run/docker.sock" + podmanSocket = "/podman/podman.sock" +) + var ( // DefaultHTTPPort Default HTTP Port used if only the protocol is provided to -H flag e.g. dockerd -H tcp:// // These are the IANA registered port numbers for use with Docker @@ -19,8 +25,7 @@ var ( // DefaultTLSHTTPPort Default HTTP Port used when TLS enabled DefaultTLSHTTPPort = 2376 // Default TLS encrypted HTTP Port // DefaultUnixSocket Path for the unix socket. - // Docker daemon by default always listens on the default unix socket - DefaultUnixSocket = "/var/run/docker.sock" + DefaultUnixSocket = getDefaultSocket() // DefaultTCPHost constant defines the default host string used by docker on Windows DefaultTCPHost = fmt.Sprintf("tcp://%s:%d", DefaultHTTPHost, DefaultHTTPPort) // DefaultTLSHost constant defines the default host string used by docker for TLS sockets @@ -166,3 +171,20 @@ func ValidateExtraHost(val string) (string, error) { } return val, nil } + +func getDefaultSocket() string { + _, err := os.Stat(dockerSocket) + if err == nil { + return dockerSocket + } + // see https://docs.podman.io/en/latest/markdown/podman-system-service.1.html#description + locations := []string{os.Getenv("XDG_RUNTIME_DIR"), "/run"} // rootless, rootful + for _, location := range locations { + _, err = os.Stat(location + podmanSocket) + if err == nil { + return location + podmanSocket + } + } + + return dockerSocket +} diff --git a/dockertest.go b/dockertest.go index 5d122249..545e5098 100644 --- a/dockertest.go +++ b/dockertest.go @@ -275,7 +275,7 @@ func NewPool(endpoint string) (*Pool, error) { endpoint = "http://localhost:2375" } } else { - endpoint = "unix:///var/run/docker.sock" + endpoint = options.DefaultHost } }