getDockerIPFromDockerHost returns the IP address of the Docker host based on the provided Docker host string.
(host string)
| 164 | |
| 165 | // getDockerIPFromDockerHost returns the IP address of the Docker host based on the provided Docker host string. |
| 166 | func getDockerIPFromDockerHost(host string) (string, error) { |
| 167 | // Default to localhost |
| 168 | hostIP := "127.0.0.1" |
| 169 | dockerHostURL, err := url.Parse(host) |
| 170 | if err != nil { |
| 171 | return hostIP, fmt.Errorf("failed to parse host=%s: %v", host, err) |
| 172 | } |
| 173 | hostPart := dockerHostURL.Hostname() |
| 174 | if hostPart == "" { |
| 175 | return hostIP, nil |
| 176 | } |
| 177 | // Check to see if the hostname we found is an IP address |
| 178 | addr := net.ParseIP(hostPart) |
| 179 | if addr == nil { |
| 180 | // If it wasn't an IP address, look it up to get IP address |
| 181 | ip, err := net.DefaultResolver.LookupIP(context.Background(), "ip4", hostPart) |
| 182 | if err == nil && len(ip) > 0 { |
| 183 | hostPart = ip[0].String() |
| 184 | } else { |
| 185 | return hostIP, fmt.Errorf("failed to look up IP address for host=%s, hostname=%s: %v", host, hostPart, err) |
| 186 | } |
| 187 | } |
| 188 | hostIP = hostPart |
| 189 | return hostIP, nil |
| 190 | } |
| 191 | |
| 192 | // ResetDockerHost resets cached Docker host data in singleton (it's not thread-safe). |
| 193 | // Used for testing only. |
no test coverage detected