-
Notifications
You must be signed in to change notification settings - Fork 254
Use Docker CLI socket from home #2171
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
//go:build darwin | ||
// +build darwin | ||
|
||
/* | ||
Copyright 2022 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) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
//go:build !e2e | ||
// +build !e2e | ||
|
||
/* | ||
Copyright 2022 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() { | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
// +build !windows | ||
//go:build !windows,!darwin | ||
// +build !windows,!darwin | ||
|
||
/* | ||
Copyright 2020 Docker Compose CLI authors | ||
Copyright 2020, 2022 Docker Compose CLI authors | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the validation currently doesn't allow for the years to differ between files, so all should have the same years There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah :( I even did a quick google for some more decent copyright header checker, but couldn't find one which does the right thing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In https://github.com/docker/cli-docs-tool/, we've been trying Google addlicense, which seemed to work (a bit more "loosely" in checking); see docker/cli-docs-tool#4 |
||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
|
@@ -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) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we should use a shorter Linux-style path in
$HOME/.docker/desktop/docker-cli.sock
even on Darwin because we've had some problems where the maximum path in the sockaddr is 104 characters (even shorter than Linux's 108 characters). We work around this in some of our binaries by callingos.Chdir("Library/Containers/com.docker.docker")
and then using a relative path... but I suspect this trick doesn't work for the CLI because changing the current directory probably breaks something.WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, this is a tough one.
The best thing I can think of from the top of my head is:
but not sure that would be safe to do.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reflecting on it a bit, I'm not as concerned as I was because the socket is best-effort (and it will work probably 99% of the time). So I'm happy with it as-is.