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/[email protected]

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 was refused - did you specify the right host or port?

Here is my kubeconfig (kind get kubeconfig):

apiVersion: v1
- cluster:
    certificate-authority-data: CERTIFICATE-AUTHORITY-DATA
  name: kind-kind
- context:
    cluster: kind-kind
    user: kind-kind
  name: kind-kind
current-context: kind-kind
kind: Config
preferences: {}
- name: kind-kind
    client-certificate-data: CLIENT-CERTIFICATE-DATA
    client-key-data: CLIENT-KEY-DATA

1 Answer 1


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


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.