Skip to content

Commit

Permalink
feat: fall back to podman if available
Browse files Browse the repository at this point in the history
  • Loading branch information
SoMuchForSubtlety committed Aug 17, 2023
1 parent f37306e commit caf88a4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
26 changes: 24 additions & 2 deletions docker/opts/hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion dockertest.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ func NewPool(endpoint string) (*Pool, error) {
endpoint = "http://localhost:2375"
}
} else {
endpoint = "unix:///var/run/docker.sock"
endpoint = options.DefaultHost
}
}

Expand Down

0 comments on commit caf88a4

Please sign in to comment.