/
host.go
40 lines (34 loc) · 989 Bytes
/
host.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 (
"os"
"strings"
)
// GetDockerHost returns the name or address of the host on which the Docker engine is running.
func GetDockerHost() string {
return getDockerHostFromEnv(os.Environ())
}
func getDockerHostFromEnv(env []string) string {
// Parses the DOCKER_HOST environment variable to find the address
//
// For valid formats see:
// https://github.com/docker/cli/blob/6916b427a0b07e8581d121967633235ced6db9a1/opts/hosts.go#L69
var dockerUrl []string
for _, item := range env {
envVar := strings.Split(item, "=")
if len(envVar) == 2 && envVar[0] == "DOCKER_HOST" {
dockerUrl = strings.Split(envVar[1], ":")
break
}
}
if len(dockerUrl) < 2 {
// DOCKER_HOST was empty, not present or not a valid URL
return "localhost"
}
switch dockerUrl[0] {
case "tcp", "ssh", "fd":
return strings.TrimPrefix(dockerUrl[1], "//")
default:
// if DOCKER_HOST is not in one of the formats listed above, return default
return "localhost"
}
}