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

Commit f922ff9

Browse files
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 f922ff9

File tree

5 files changed

+93
-7
lines changed

5 files changed

+93
-7
lines changed

cli/metrics/conn_darwin.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//go:build darwin
2+
// +build darwin
3+
4+
/*
5+
Copyright 2022 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+
overrideSocket() // nop, unless built for e2e testing
39+
}
40+
41+
func conn() (net.Conn, error) {
42+
return net.Dial("unix", socket)
43+
}

cli/metrics/conn_e2e.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
//go:build e2e
12
// +build e2e
23

34
/*
4-
Copyright 2020 Docker Compose CLI authors
5+
Copyright 2020, 2022 Docker Compose CLI authors
56
67
Licensed under the Apache License, Version 2.0 (the "License");
78
you may not use this file except in compliance with the License.
@@ -22,7 +23,7 @@ import (
2223
"os"
2324
)
2425

25-
func init() {
26+
func overrideSocket() {
2627
testSocket, defined := os.LookupEnv("TEST_METRICS_SOCKET")
2728
if defined {
2829
socket = testSocket

cli/metrics/conn_note2e.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//go:build !e2e
2+
// +build !e2e
3+
4+
/*
5+
Copyright 2022 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+
func overrideSocket() {
23+
}

cli/metrics/conn_other.go

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

34
/*
4-
Copyright 2020 Docker Compose CLI authors
5+
Copyright 2020, 2022 Docker Compose CLI authors
56
67
Licensed under the Apache License, Version 2.0 (the "License");
78
you may not use this file except in compliance with the License.
@@ -18,12 +19,25 @@
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+
overrideSocket() // nop, unless built for e2e testing
39+
}
40+
2741
func conn() (net.Conn, error) {
2842
return net.Dial("unix", socket)
2943
}

cli/metrics/conn_windows.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
//go:build windows
12
// +build windows
23

34
/*
4-
Copyright 2020 Docker Compose CLI authors
5+
Copyright 2020, 2022 Docker Compose CLI authors
56
67
Licensed under the Apache License, Version 2.0 (the "License");
78
you may not use this file except in compliance with the License.
@@ -30,6 +31,10 @@ var (
3031
socket = `\\.\pipe\docker_cli`
3132
)
3233

34+
func init() {
35+
overrideSocket() // no-op, unless built for e2e testing
36+
}
37+
3338
func conn() (net.Conn, error) {
3439
if strings.HasPrefix(socket, `\\.\pipe\`) {
3540
timeout := 200 * time.Millisecond

0 commit comments

Comments
 (0)