Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.

Commit 0dee372

Browse files
author
Piotr Stankiewicz
committed
Use Docker CLI socket from the User's home directory
We should not rely on having a global path for the Docker CLI socket. On macOS this forces Docker Desktop to access directories which require raised privileges. Whereas on Linux we do not create sockets in that location at all, currently. So look for the Docker CLI socket in the User's home directory. Signed-off-by: Piotr Stankiewicz <[email protected]>
1 parent 6f3f942 commit 0dee372

File tree

2 files changed

+58
-3
lines changed

2 files changed

+58
-3
lines changed

cli/metrics/conn_darwin.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//go:build darwin
2+
// +build darwin
3+
4+
/*
5+
Copyright 2020 Docker Compose CLI authors
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
*/
19+
20+
package metrics
21+
22+
import (
23+
"net"
24+
"path/filepath"
25+
26+
"github.com/docker/docker/pkg/homedir"
27+
)
28+
29+
var (
30+
socket = "/var/run/docker-cli.sock"
31+
)
32+
33+
func init() {
34+
// Attempt to retrieve the Docker CLI socket for the current user.
35+
if home := homedir.Get(); home != "" {
36+
socket = filepath.Join(home, "/Library/Containers/com.docker.docker/Data/docker-cli.sock")
37+
} // else: On macOS Docker Desktop creates symlinks in /var/run, so fall back to the old default.
38+
}
39+
40+
func conn() (net.Conn, error) {
41+
return net.Dial("unix", socket)
42+
}

cli/metrics/conn_other.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
// +build !windows
1+
//go:build !windows,!darwin
2+
// +build !windows,!darwin
23

34
/*
45
Copyright 2020 Docker Compose CLI authors
@@ -18,12 +19,24 @@
1819

1920
package metrics
2021

21-
import "net"
22+
import (
23+
"net"
24+
"path/filepath"
25+
26+
"github.com/docker/docker/pkg/homedir"
27+
)
2228

2329
var (
24-
socket = "/var/run/docker-cli.sock"
30+
socket = ""
2531
)
2632

33+
func init() {
34+
// Attempt to retrieve the Docker CLI socket for the current user.
35+
if home := homedir.Get(); home != "" {
36+
socket = filepath.Join(home, ".docker/desktop/docker-cli.sock")
37+
} // else: On Linux we don't expect to have a global CLI socket, so leave it empty and let connections fail.
38+
}
39+
2740
func conn() (net.Conn, error) {
2841
return net.Dial("unix", socket)
2942
}

0 commit comments

Comments
 (0)