<p>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.</p>
<p>So if you run <code>kubectl</code> commands from your machine it will work (including the kubeconfig).</p>
<p>Solution: Using <code>--network=&quot;host&quot;</code> in your docker command, the container will act as your localhost (eg. <code>docker run --network=&quot;host&quot; -v /var/run/docker.sock:/var/run/docker.sock -it ef33f1a0ac5e bash</code></p>

CC BY-SA 4.0
2021-01-03T16:35:17.537Z
2021-01-03T16:35:17.537Z
AdrianB
<p>For CICD purposes, I need to run a docker container and then create a kubernetes cluster inside of it using <a href="https://kind.sigs.k8s.io/" rel="nofollow noreferrer">kind</a>. Here is my Dockerfile:</p>
<pre><code>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
</code></pre>
<p>It's basically an ubuntu image with kind installed.</p>
<p>I run this image using docker run commad:</p>
<pre><code>docker run -v /var/run/docker.sock:/var/run/docker.sock -it ef33f1a0ac5e bash
</code></pre>
<p>And then I create a kind cluster inside of it:</p>
<pre><code>kind create cluster --name kind
</code></pre>
<p>Then I <a href="https://kubernetes.io/docs/tasks/tools/install-kubectl/" rel="nofollow noreferrer">install kubectl</a> on it.</p>
<p>When I try to connect to my kind cluster using kubectl (<code>kubectl get all</code>, for example), I get the following error:</p>
<pre><code>The connection to the server 127.0.0.1:37583 was refused - did you specify the right host or port?
</code></pre>
<p>Here is my kubeconfig (<code>kind get kubeconfig</code>):</p>
<pre><code>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
</code></pre>

<docker><kubernetes>
How to connect to a kubernetes cluster that is inside of a docker container?
HsnVahedi
# How to connect to a kubernetes cluster that is inside of a docker container?

(Asked by: username_0 on 2021-01-02)

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 "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
RUN apt-get update && 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="on" /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

**Tags:** docker, kubernetes


---

## 1 answers

### Answer by username_1 on 2021-01-03. Score: 2 | Accepted answer

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="host"` in your docker command, the container will act as your localhost (eg. `docker run --network="host" -v /var/run/docker.sock:/var/run/docker.sock -it ef33f1a0ac5e bash`


---

