<p>I have a running <code>minikube</code> cluster. I can easily connect to it and apply changes using <code>kubectl</code>. But I want to run <code>kubectl</code> from a docker container. Here is the Dockerfile:</p>
<pre><code>FROM alpine:latest

RUN apk --no-cache add curl

# Install and configure kubectl
RUN curl -LO &quot;https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl&quot;
RUN mkdir -p ~/.local/bin/kubectl
RUN mv ./kubectl ~/.local/bin/kubectl
RUN chmod +x ~/.local/bin/kubectl/ -R
</code></pre>
<p>It's basically a simple <code>alpine</code> image with <code>kubectl</code> installed.</p>
<p>How can I connect to my <code>minikube</code> cluster from this container?</p>

<p>I had to copy <code>~/.kube</code> and <code>~/.minikube</code> folders into the image. This is the new Dockerfile:</p>
<pre><code>FROM alpine:latest

RUN apk --no-cache add curl

# Install and configure kubectl
RUN curl -LO &quot;https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl&quot;
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/
</code></pre>
<p>You can use the image like this:</p>
<pre><code>docker build . -t USERNAME/kubectl:latest
docker run USERNAME/kubectl:latest ./kubectl get pods
</code></pre>
<p><em><strong>ATTENTION</strong></em></p>
<p>The <code>.kube/config</code> file is created for the host system. So you need to change some paths in <code>.kube/config</code> file to point to the <code>.minikube</code> folder in the container.</p>
<p><em><strong>ALSO NOTE THAT</strong></em></p>
<p><code>~/.minikube</code> and <code>~/.kube</code> are huge folders. Adding them to your docker build context could make your builds really slow.</p>
<p>You might want to mount volumes for that purpose.</p>
