github.com/mponton/terratest@v0.44.0/modules/docker/host.go (about) 1 package docker 2 3 import ( 4 "os" 5 "strings" 6 ) 7 8 // GetDockerHost returns the name or address of the host on which the Docker engine is running. 9 func GetDockerHost() string { 10 return getDockerHostFromEnv(os.Environ()) 11 } 12 13 func getDockerHostFromEnv(env []string) string { 14 // Parses the DOCKER_HOST environment variable to find the address 15 // 16 // For valid formats see: 17 // https://github.com/docker/cli/blob/6916b427a0b07e8581d121967633235ced6db9a1/opts/hosts.go#L69 18 var dockerUrl []string 19 20 for _, item := range env { 21 envVar := strings.Split(item, "=") 22 if len(envVar) == 2 && envVar[0] == "DOCKER_HOST" { 23 dockerUrl = strings.Split(envVar[1], ":") 24 break 25 } 26 } 27 28 if len(dockerUrl) < 2 { 29 // DOCKER_HOST was empty, not present or not a valid URL 30 return "localhost" 31 } 32 33 switch dockerUrl[0] { 34 case "tcp", "ssh", "fd": 35 return strings.TrimPrefix(dockerUrl[1], "//") 36 default: 37 // if DOCKER_HOST is not in one of the formats listed above, return default 38 return "localhost" 39 } 40 }