HsnVahedi
HsnVahedi

Reputation: 1366

How to connect to a minikube cluster from a docker container?

I have a running minikube cluster. I can easily connect to it and apply changes using kubectl. But I want to run kubectl from a docker container. Here is the Dockerfile:

FROM alpine:latest

RUN apk --no-cache add curl

# Install and configure kubectl
RUN curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
RUN mkdir -p ~/.local/bin/kubectl
RUN mv ./kubectl ~/.local/bin/kubectl
RUN chmod +x ~/.local/bin/kubectl/ -R

It's basically a simple alpine image with kubectl installed.

How can I connect to my minikube cluster from this container?

Upvotes: 1

Views: 2130

Answers (1)

HsnVahedi
HsnVahedi

Reputation: 1366

I had to copy ~/.kube and ~/.minikube folders into the image. This is the new Dockerfile:

FROM alpine:latest

RUN apk --no-cache add curl

# Install and configure kubectl
RUN curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
RUN mkdir -p ~/.local/bin/kubectl
RUN mv ./kubectl ~/.local/bin/kubectl
COPY .kube /root/.kube
COPY .minikube /root/.minikube
RUN chmod +r ~/.kube/config
RUN chmod +x ~/.local/bin/kubectl/ -R
WORKDIR /root/.local/bin/kubectl/

You can use the image like this:

docker build . -t USERNAME/kubectl:latest
docker run USERNAME/kubectl:latest ./kubectl get pods

ATTENTION

The .kube/config file is created for the host system. So you need to change some paths in .kube/config file to point to the .minikube folder in the container.

ALSO NOTE THAT

~/.minikube and ~/.kube are huge folders. Adding them to your docker build context could make your builds really slow.

You might want to mount volumes for that purpose.

Upvotes: 2

Related Questions