How to connect to a kubernetes cluster that is inside of a docker container? For CICD purposes, I need to run a docker container and then create a kubernetes cluster inside of it using kind. Here is my Dockerfile:
FROM ubuntu:18.04

RUN apt-get update
RUN apt-get install curl -y

RUN apt-get install -y gnupg2

RUN curl -fsSL https://apt.releases.hashicorp.com/gpg | apt-key add -
RUN apt-get install -y lsb-release
RUN apt-get install -y software-properties-common
RUN apt-add-repository &quot;deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main&quot;
RUN apt-get update &amp;&amp; apt-get install terraform -y

RUN apt-get install -y wget
RUN wget https://golang.org/dl/go1.15.6.linux-amd64.tar.gz
RUN tar -C /usr/local -xzf go1.15.6.linux-amd64.tar.gz
RUN GO111MODULE=&quot;on&quot; /usr/local/go/bin/go get sigs.k8s.io/kind@v0.9.0

RUN curl -fsSL https://get.docker.com -o get-docker.sh
RUN sh get-docker.sh

It's basically an ubuntu image with kind installed.
I run this image using docker run commad:
docker run -v /var/run/docker.sock:/var/run/docker.sock -it ef33f1a0ac5e bash

And then I create a kind cluster inside of it:
kind create cluster --name kind

Then I install kubectl on it.
When I try to connect to my kind cluster using kubectl (kubectl get all, for example), I get the following error:
The connection to the server 127.0.0.1:37583 was refused - did you specify the right host or port?

Here is my kubeconfig (kind get kubeconfig):
apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: CERTIFICATE-AUTHORITY-DATA
    server: https://127.0.0.1:37583
  name: kind-kind
contexts:
- context:
    cluster: kind-kind
    user: kind-kind
  name: kind-kind
current-context: kind-kind
kind: Config
preferences: {}
users:
- name: kind-kind
  user:
    client-certificate-data: CLIENT-CERTIFICATE-DATA
    client-key-data: CLIENT-KEY-DATA
While you are running a container linked to your PC/Server Docker, the Kind Kubernetes port is available on your PC/Server not on the container.
So if you run kubectl commands from your machine it will work (including the kubeconfig).
Solution: Using --network=&quot;host&quot; in your docker command, the container will act as your localhost (eg. docker run --network=&quot;host&quot; -v /var/run/docker.sock:/var/run/docker.sock -it ef33f1a0ac5e bash