Skip to content

Commit 5ec3f84

Browse files
committed
fix: default to unix scheme when DOCKER_HOST is a path
When DOCKER_HOST is set to a filesystem path without a scheme (e.g. /var/run/docker.sock), parseDockerDaemonHost previously defaulted to the "tcp" protocol, producing a confusing error: "Cannot connect to the Docker daemon at tcp://localhost:2375/foo.sock" Now, if the address starts with "/" or "./", the parser defaults to "unix" instead of "tcp", which matches the user's intent and produces a correct connection or a clear error. Fixes #5846 Signed-off-by: Aaron Mark <64331623+amarkdotdev@users.noreply.github.com>
1 parent 1d1562e commit 5ec3f84

2 files changed

Lines changed: 10 additions & 1 deletion

File tree

opts/hosts.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,14 @@ func parseDockerDaemonHost(addr string) (string, error) {
5757
proto, host, hasProto := strings.Cut(addr, "://")
5858
if !hasProto && proto != "" {
5959
host = proto
60-
proto = "tcp"
60+
// If the address looks like a filesystem path, default to "unix"
61+
// rather than "tcp" to avoid confusing errors when the user sets
62+
// DOCKER_HOST=/path/to/sock without a unix:// prefix.
63+
if strings.HasPrefix(host, "/") || strings.HasPrefix(host, "./") {
64+
proto = "unix"
65+
} else {
66+
proto = "tcp"
67+
}
6168
}
6269

6370
switch proto {

opts/hosts_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ func TestParseDockerDaemonHost(t *testing.T) {
7979
"unix://": "unix://" + defaultUnixSocket,
8080
"fd://": "fd://",
8181
"fd://something": "fd://something",
82+
"/var/run/docker.sock": "unix:///var/run/docker.sock",
83+
"./run/docker.sock": "unix://run/docker.sock",
8284
"localhost:": "tcp://localhost:2375",
8385
"localhost:5555": "tcp://localhost:5555",
8486
"localhost:5555/path": "tcp://localhost:5555/path",

0 commit comments

Comments
 (0)