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

Commit

Permalink
Use Docker CLI socket from the User's home directory
Browse files Browse the repository at this point in the history
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 <piotr.stankiewicz@docker.com>
  • Loading branch information
p1-0tr committed Jul 8, 2022
1 parent 6f3f942 commit 1504064
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 4 deletions.
43 changes: 43 additions & 0 deletions cli/metrics/conn_darwin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//go:build darwin
// +build darwin

/*
Copyright 2020 Docker Compose CLI authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package metrics

import (
"net"
"path/filepath"

"github.com/docker/docker/pkg/homedir"
)

var (
socket = "/var/run/docker-cli.sock"
)

func init() {
// Attempt to retrieve the Docker CLI socket for the current user.
if home := homedir.Get(); home != "" {
socket = filepath.Join(home, "/Library/Containers/com.docker.docker/Data/docker-cli.sock")
} // else: On macOS Docker Desktop creates symlinks in /var/run, so fall back to the old default.
overrideSocket() // nop, unless built for e2e testing
}

func conn() (net.Conn, error) {
return net.Dial("unix", socket)
}
3 changes: 2 additions & 1 deletion cli/metrics/conn_e2e.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build e2e
// +build e2e

/*
Expand All @@ -22,7 +23,7 @@ import (
"os"
)

func init() {
func overrideSocket() {
testSocket, defined := os.LookupEnv("TEST_METRICS_SOCKET")
if defined {
socket = testSocket
Expand Down
23 changes: 23 additions & 0 deletions cli/metrics/conn_note2e.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//go:build !e2e
// +build !e2e

/*
Copyright 2020 Docker Compose CLI authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package metrics

func overrideSocket() {
}
20 changes: 17 additions & 3 deletions cli/metrics/conn_other.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// +build !windows
//go:build !windows,!darwin
// +build !windows,!darwin

/*
Copyright 2020 Docker Compose CLI authors
Expand All @@ -18,12 +19,25 @@

package metrics

import "net"
import (
"net"
"path/filepath"

"github.com/docker/docker/pkg/homedir"
)

var (
socket = "/var/run/docker-cli.sock"
socket = ""
)

func init() {
// Attempt to retrieve the Docker CLI socket for the current user.
if home := homedir.Get(); home != "" {
socket = filepath.Join(home, ".docker/desktop/docker-cli.sock")
} // else: On Linux we don't expect to have a global CLI socket, so leave it empty and let connections fail.
overrideSocket() // nop, unless built for e2e testing
}

func conn() (net.Conn, error) {
return net.Dial("unix", socket)
}
5 changes: 5 additions & 0 deletions cli/metrics/conn_windows.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build windows
// +build windows

/*
Expand Down Expand Up @@ -30,6 +31,10 @@ var (
socket = `\\.\pipe\docker_cli`
)

func init() {
overrideSocket() // nop, unless built for e2e testing
}

func conn() (net.Conn, error) {
if strings.HasPrefix(socket, `\\.\pipe\`) {
timeout := 200 * time.Millisecond
Expand Down

0 comments on commit 1504064

Please sign in to comment.