Can't run kubectl in docker container from a host machine installed Minikube "The connection to the server 127.0.0.1:32768 was refused"

1/5/2021

I want to have a container that can access and run kubectl command on my host machine. Here is what I have:

  • I have installed Kubernetes and Minikube on my host machine.
  • I used this docker container: helm-kubectl link

This is the command I run my docker:

docker run -it -v ~/.kube:/root/.kube -v ~/.minikube:/Users/xxxx/.minikube dtzar/helm-kubectl

Inside the container, when I checked the cluster, I can see the context has loaded my minikube, However, I can't run another kubectl command due to the reason "The connection to the server 127.0.0.1:32768 was refused - did you specify the right host or port?".

bash-5.0# kubectl config get-contexts
CURRENT   NAME                                                           CLUSTER                                                        AUTHINFO                                                       NAMESPACE
          docker-desktop                                                 docker-desktop                                                 docker-desktop
          docker-for-desktop                                             docker-desktop                                                 docker-desktop
*         minikube                                                       minikube                                                       minikube
bash-5.0# kubectl get all
The connection to the server 127.0.0.1:32768 was refused - did you specify the right host or port?

I have checked my Kubenetes config at ~/.kube and the port is 32768.

- cluster:
    certificate-authority: /Users/xxx/.minikube/ca.crt
    server: https://127.0.0.1:32768
  name: minikube

I have tried port -p 32768 or --expose 32768 but no luck. So anyone can help this?

-- Tianbing Leng
docker
kubectl
kubernetes
macos

1 Answer

1/5/2021

Thanks zerkms! It works with --network host

-- Tianbing Leng
Source: StackOverflow