/
docker.go
40 lines (32 loc) · 1.46 KB
/
docker.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package docker
import (
"strings"
)
const ContainerLabelRole = "dev.tilt.ctlptl.role"
// Checks whether the Docker daemon is running on a local machine.
// Remote docker daemons will likely need a port forwarder to work properly.
func IsLocalHost(dockerHost string) bool {
return dockerHost == "" ||
// Check all the "standard" docker localhosts.
// https://github.com/docker/cli/blob/a32cd16160f1b41c1c4ae7bee4dac929d1484e59/opts/hosts.go#L22
strings.HasPrefix(dockerHost, "tcp://localhost:") ||
strings.HasPrefix(dockerHost, "tcp://127.0.0.1:") ||
// https://github.com/moby/moby/blob/master/client/client_windows.go#L4
strings.HasPrefix(dockerHost, "npipe:") ||
// https://github.com/moby/moby/blob/master/client/client_unix.go#L6
strings.HasPrefix(dockerHost, "unix:")
}
// Checks whether the DOCKER_HOST looks like a local Docker Engine.
// A local Docker Engine has some additional APIs for VM management (i.e., Docker Desktop).
func IsLocalDockerEngineHost(dockerHost string) bool {
if strings.HasPrefix(dockerHost, "unix:") {
// Many tools (like colima) try to masquerade as Docker Desktop but run
// on a different socket.
// see:
// https://github.com/tilt-dev/ctlptl/issues/196
// https://docs.docker.com/desktop/faqs/#how-do-i-connect-to-the-remote-docker-engine-api
return strings.Contains(dockerHost, "/var/run/docker.sock")
}
// Docker daemons on other local protocols are treated as docker desktop.
return IsLocalHost(dockerHost)
}