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 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()
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 != "" {
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.HostIP = machineIP
} else {
log.Traceln("Not using docker-machine")
}
}

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

Loading…
Cancel
Save