<p>I am trying to run this <a href="https://github.com/monzo/egress-operator/blob/295f3661989e4b9331ba5241cdb4f256287203c4/Dockerfile#L22" rel="nofollow noreferrer">Dockerfile with distroless image</a> (<code>gcr.io/distroless/static:nonroot</code>). <code>docker build</code> is happening successfully, but <code>docker run -it image_name</code> is giving me error:</p>
<pre><code>2021-07-13T18:16:11.441Z   ERROR   controller-runtime.client.config  unable to get kubeconfig    {&quot;error&quot;: &quot;could not locate a kubeconfig&quot;}
github.com/go-logr/zapr.(*zapLogger).Error
  /go/pkg/mod/github.com/go-logr/zapr@v0.1.0/zapr.go:128
sigs.k8s.io/controller-runtime/pkg/client/config.GetConfigOrDie
  /go/pkg/mod/sigs.k8s.io/controller-runtime@v0.4.0/pkg/client/config/config.go:146
main.main
  /workspace/main.go:63
runtime.main
  /usr/local/go/src/runtime/proc.go:203
</code></pre>
<p><strong>Debugging findings</strong></p>
<ol>
<li>Keeping distroless image if I am removing last line <code>ENTRYPOINT [&quot;/manager&quot;]</code> then <code>docker run -it image_name</code> giving error as-: <code>docker: Error response from daemon: No command specified. See 'docker run --help'</code>. <br/>This same <code>docker run</code> command working for distroless(with ENTRYPOINT line) but not working with distroless(without ENTRYPOINT line)</li>
<li>I replaced distroless image with <code>alpine:latest</code>. Here with <code>ENTRYPOINT [&quot;/manager&quot;]</code> (&amp; without <code>USER nonroot:nonroot</code>) I am seeing same error as above <code>ERROR controller-runtime.client.config  unable to get kubeconfig...</code> <strong>BUT</strong> without ENTRYPOINT line, I am able to login to container with <code>docker run -it image_name</code>.</li>
</ol>
<p>Someone please let me know how to resolve this, so that I can make this dockerfile run with all required configs as in Dockerfile.</p>
<p><strong>NOTE:</strong> I am afraid that my egress-operator pod might not run by changing image name, as it can lead to miss any configuration in dockerfile in order to make it run.</p>

solveit
<p>Short answer:</p>
<p>If you want to run your image, just do this:</p>
<p>you have 2 options for it:</p>
<ol>
<li>Run your image inside a Kubernetes Cluster</li>
<li>Place your kubeconfig inside your image as <code>$HOME/.kube/config</code></li>
</ol>
<p>If you are trying to debug your image, try this:</p>
<pre class="lang-sh prettyprint-override"><code>docker run --rm -it --entrypoint bash image_name
</code></pre>
<p>replace <code>bash</code> with <code>sh</code> if <code>command not found</code> happened.</p>
<hr />
<h2 id="explanation-oeun">Explanation</h2>
<h3 id="dockerfile-part-7g7h">Dockerfile part</h3>
<p>According to the <a href="https://docs.docker.com/engine/reference/builder/#entrypoint" rel="nofollow noreferrer">Dockerfile Docs about entrypoint</a>,</p>
<blockquote>
<p>An ENTRYPOINT allows you to configure a container that will run as an executable. Command line arguments to <code>docker run &lt;image&gt;</code> will be appended after all elements in an exec form ENTRYPOINT</p>
</blockquote>
<p>Your command is <code>docker run -it image_name</code> without any arg, so that docker will treat it as:</p>
<ol>
<li>Run it in the image env</li>
<li>Run entrypoint which is <code>/manager</code></li>
</ol>
<p>the <code>/manager</code> is built with <code>kubebuilder</code>, it will try to load the <code>kubeconfig</code> and die if not thing found.</p>
<p>If you do not want to run the <code>/manager</code> when execute <code>docker run</code>, you have to replace it by using <code>--entrypoint</code> arg.</p>
<h3 id="kubebuilder-part-30a4">Kubebuilder part</h3>
<p>As I noticed you mentioned you afraid of missing the kubeconfig for your pod, appending this edit.</p>
<p>kubebuilder would try to find kubeconfig in 2 place by default:</p>
<ol>
<li><code>$HOME/.kube/config</code>: the configuration file in the file system.</li>
<li>in-cluster: a pod running inside kubernetes can get an in-cluster kubeconfig.
<code>/</code></li>
</ol>
