do not use host.docker.internal IP for API on non-docker-machine setups of Docker for Desktop

pull/405/head v3.2.1
iwilltry42 4 years ago
parent 437f53a72c
commit aa3a54e3c9
No known key found for this signature in database
GPG Key ID: 7BA57AD1CFF16110
  1. 8
      pkg/cluster/cluster.go
  2. 52
      pkg/runtimes/docker/machine.go

@ -65,14 +65,16 @@ func ClusterCreate(ctx context.Context, runtime k3drt.Runtime, cluster *k3d.Clus
if cluster.ExposeAPI.Host == k3d.DefaultAPIHost && runtime == k3drt.Docker { if cluster.ExposeAPI.Host == k3d.DefaultAPIHost && runtime == k3drt.Docker {
if gort.GOOS == "windows" || gort.GOOS == "darwin" { if gort.GOOS == "windows" || gort.GOOS == "darwin" {
log.Tracef("Running on %s -> Trying to get IP of the docker machine", gort.GOOS) log.Tracef("Running on %s: checking if it's using docker-machine", gort.GOOS)
machineIP, err := runtime.(docker.Docker).GetDockerMachineIP() machineIP, err := runtime.(docker.Docker).GetDockerMachineIP()
if err != nil { if err != nil {
log.Warnf("Failed to get Docker Machine IP: %+v", err) log.Warnf("Using docker-machine, but failed to get it's IP: %+v", err)
} else if machineIP != "" { } else if machineIP != "" {
log.Infof("Using the docker machine IP %s to connect to the Kubernetes API", machineIP) log.Infof("Using the docker-machine IP %s to connect to the Kubernetes API", machineIP)
cluster.ExposeAPI.Host = machineIP cluster.ExposeAPI.Host = machineIP
cluster.ExposeAPI.HostIP = machineIP cluster.ExposeAPI.HostIP = machineIP
} else {
log.Traceln("Not using docker-machine")
} }
} }

@ -23,8 +23,6 @@ THE SOFTWARE.
package docker package docker
import ( import (
"fmt"
"net"
"os" "os"
"os/exec" "os/exec"
"strings" "strings"
@ -34,41 +32,33 @@ import (
func (d Docker) GetDockerMachineIP() (string, error) { func (d Docker) GetDockerMachineIP() (string, error) {
machine := os.ExpandEnv("$DOCKER_MACHINE_NAME") machine := os.ExpandEnv("$DOCKER_MACHINE_NAME")
dockerHostName := "host.docker.internal"
// Option 1: use the docker-machine executable
if machine != "" { if machine != "" {
dockerMachinePath, err := exec.LookPath("docker-machine") log.Debugf("Docker Machine found: %s", machine)
if err != nil { }
return "", err
}
out, err := exec.Command(dockerMachinePath, "ip", machine).Output()
if err != nil {
log.Printf("Error executing 'docker-machine ip'")
if exitError, ok := err.(*exec.ExitError); ok { dockerMachinePath, err := exec.LookPath("docker-machine")
log.Printf("%s", string(exitError.Stderr)) if err != nil {
if err == exec.ErrNotFound {
if machine != "" {
log.Debugf("DOCKER_MACHINE_NAME env var present, but executable docker-machine not found: %w", err)
} else {
log.Tracef("docker-machine executable not found: %w", err)
} }
return "", err
} }
ipStr := strings.TrimSuffix(string(out), "\n") return "", nil
ipStr = strings.TrimSuffix(ipStr, "\r")
return ipStr, nil
} }
// Option 2: Try to lookup "host.docker.internal" out, err := exec.Command(dockerMachinePath, "ip", machine).Output()
log.Debugf("Docker Machine not found, trying to lookup '%s' instead...", dockerHostName)
addrs, err := net.LookupHost(dockerHostName)
if err != nil { if err != nil {
log.Debugf("Lookup of Host %s failed: %+v", dockerHostName, err) log.Printf("Error executing 'docker-machine ip'")
return "", fmt.Errorf("Failed to get IP of Docker VM")
} if exitError, ok := err.(*exec.ExitError); ok {
if len(addrs) == 0 { log.Printf("%s", string(exitError.Stderr))
log.Debugf("Lookup of Host %s didn't return any addresses", dockerHostName) }
return "", fmt.Errorf("Failed to get IP of Docker VM") return "", err
} }
log.Debugf("Addresses returned for %s: %+v", dockerHostName, addrs) ipStr := strings.TrimSuffix(string(out), "\n")
return addrs[0], nil ipStr = strings.TrimSuffix(ipStr, "\r")
return ipStr, nil
} }

Loading…
Cancel
Save