kubectl version command on Docker Desktop for Mac always returns a "Unable to connect to the server: dial tcp...no such host" error

Created on 28 May 2020  路  6Comments  路  Source: kubernetes/kubectl

I have just started using Kubernetes on my Docker Desktop for Mac and after enabling Kubernetes from the Preferences window, I tried running kubectl version to get the client version and server version. This is what I saw instead:

$kubectl version
Client Version: version.Info{Major:"1", Minor:"16", GitVersion:"v1.16.7", GitCommit:"be3d344ed06bff7a4fc60656200a93c74f31f9a4", GitTreeState:"clean", BuildDate:"2020-02-11T19:34:02Z", GoVersion:"go1.13.6", Compiler:"gc", Platform:"darwin/amd64"}
Unable to connect to the server: dial tcp: lookup kubernetes.docker.internal on 174.141.176.114:53: no such host

I then decided to take a look at the kubectl config file by running kubectl config view:

$kubectl config view
apiVersion: v1
clusters:

  • cluster:
    certificate-authority-data: DATA+OMITTED
    server: https://kubernetes.docker.internal:6443
    name: docker-desktop
    contexts:

    • context:

      cluster: docker-desktop

      user: docker-desktop

      name: docker-desktop

    • context:

      cluster: docker-desktop

      user: docker-desktop

      name: docker-for-desktop

      current-context: docker-for-desktop

      kind: Config

      preferences: {}

      users:

    • name: docker-desktop

      user:

      client-certificate-data: REDACTED

      client-key-data: REDACTED

Prior to this, I had already changed my current context to docker-for-desktop (as you can see in the above config file) but after running kubectl version, I kept seeing the same "Unable to connect to server error". That is very interesting because the above config file shows another address for the server....

After perusing some online threads, I decided to look at the cluster by running kubectl cluster-info. But this also gave me the same error:

$kubectl cluster-info
To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
Unable to connect to the server: dial tcp: lookup kubernetes.docker.internal on 174.141.176.114:53: no such host

I ran kubectl cluster-info dump and saw the same error yet again:

$kubectl cluster-info dump
Unable to connect to the server: dial tcp: lookup kubernetes.docker.internal on 174.141.176.114:53: no such host

I have already restarted Docker Desktop and reset the Kubernetes cluster but the error unfortunately doesn't go away.

Please advise and let me know the best way to solve this issue so that I can use kubectl to run Kubernetes commands locally with Docker Desktop on my Mac laptop.

Here is also a screenshot showing the version of Docker Desktop for Mac that I am currently running:

Screen Shot 2020-05-28 at 2 25 04 AM

kinsupport

Most helpful comment

Just figured it out. By default, the server address for the cluster in the kube config file is set to https://kubernetes.docker.internal:[port number]. For Docker Desktop for Mac version 2.3.0.3 and K8s version 1.16.5, this server address needs to be changed to https://localhost:[port number], where the port number is usually something like 6443. Once I made this change to the server address, I was finally able to connect to the local kube server and use the kubectl command without any issues.

You can change the kube config file by finding the .kube folder in the Users folder on your Mac (it's a hidden folder) and opening the config file in an editor. Then change the server address in the config file and save it.

All 6 comments

Just figured it out. By default, the server address for the cluster in the kube config file is set to https://kubernetes.docker.internal:[port number]. For Docker Desktop for Mac version 2.3.0.3 and K8s version 1.16.5, this server address needs to be changed to https://localhost:[port number], where the port number is usually something like 6443. Once I made this change to the server address, I was finally able to connect to the local kube server and use the kubectl command without any issues.

You can change the kube config file by finding the .kube folder in the Users folder on your Mac (it's a hidden folder) and opening the config file in an editor. Then change the server address in the config file and save it.

Glad you got it figured out.
If the server address is wrong, you might want to open an issue with Docker Desktop for Mac. They would be the ones who set that address.

/triage support
/close

@eddiezane: Closing this issue.

In response to this:

/triage support
/close

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

This resolved my issue. Thank you.

Just in case anyone encounters the same issue I had... Editing the kube config file from kubernetes.docker.internal to localhost worked, but was getting overwritten each time I restarted kube. In the end I found that there was something wrong with my /etc/hosts file - it had an entry for kubernetes.docker.internal, but I couldn't ping it. I recreated the /etc/hosts file from scratch and I could then ping kubernetes.docker.internal. Kubectl then started working.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ameukam picture ameukam  路  5Comments

cbluth picture cbluth  路  6Comments

whs-dot-hk picture whs-dot-hk  路  6Comments

pwittrock picture pwittrock  路  6Comments

armujahid picture armujahid  路  4Comments