<p>I'm trying to containerize a python application, where I used the Kubernetes package. My Dockerfile is:</p>
<pre class="lang-bash prettyprint-override"><code>FROM python:3.10.6

ADD App_pod.py .
ADD config.yml ~/.kube/config

RUN pip3 install requests beautifulsoup4
RUN pip3 install kubernetes
RUN apt-get install -y ca-certificates curl

CMD [&quot;python3&quot;,&quot;./App_pod.py&quot;]
</code></pre>
<p>when I enter docker run -i -t run, it shows me the following error:</p>
<pre class="lang-none prettyprint-override"><code>&gt; Traceback (most recent call last):
&gt; File &quot;//./App_pod.py&quot;, line 9, in &lt;module&gt;
&gt; config.load_kube_config()
&gt; File &quot;/usr/local/lib/python3.10/site-packages/kubernetes/config/kube_config.py&quot;, line 813, in load_kube_config
&gt; loader = _get_kube_config_loader(
&gt; File &quot;/usr/local/lib/python3.10/site-packages/kubernetes/config/kube_config.py&quot;, line 770, in _get_kube_config_loader
&gt; raise ConfigException(
&gt; kubernetes.config.config_exception.ConfigException: Invalid kube-config file. No configuration found.
</code></pre>
<p>I tried to change the appearing error from raise exeption but it still shows me the same error, so I guess maybe it doesn't detect the changes I do.</p>
<p>I used ADD command to copy the config file to the specific path, but it still shows the same error. I also tried to mount the volume but it can't be mounted since there is no container yet, because it needs first the conf file to be created. Does anyone face this problem and know how to fix it?</p>

Mahmoud Abdelhafez
<p>A couple of issues:</p>
<ul>
<li><code>~</code> has a special meaning on Linux; it is a shortcut to the user's home directory. I think <code>~</code> is not available for the destination path of a Dockerfile <code>ADD</code> (or <code>COPY</code>) command. You should use a specific path instead</li>
<li>It's not advisable to include a file that includes credentials (such as a kubeconfig file) in a container. You should instead mount data such as this.</li>
<li>You're renaming the file <code>config.yml</code> is the source but <code>config</code> is the destination which may be problematic.</li>
</ul>
<p>You don't include the full Dockerfile so it's unclear what your <code>FROM</code> image is and you don't include the source of your <code>App_pod.py</code> file so it's unclear where it looks for the kubeconfig file, but...</p>
<ol>
<li>Generalize your code's use of the kubeconfig file by providing it as an argument or using an environment variable. This way, the location is dynamic and the code can work when run outside of a container or within</li>
<li>Mount the file into the container when you <code>run</code> the container</li>
</ol>
<p>Let's assume you change your code to accept a command-line argument for <code>config.yml</code> in the working directory, e.g.:</p>
<pre class="lang-bash prettyprint-override"><code>python3 App_pod.py ~/.kube/config
</code></pre>
<p>Then, when you run the container, you will need to mount the <code>config.yml</code> into the container and reference it. In the following, I'm using different source and destination folders to demonstrate the point:</p>
<pre class="lang-bash prettyprint-override"><code>docker run \
--interactive --tty --rm \
--volume=~/.kube/config:/somewhere/.kube/config \
your-container /somewhere/.kube/config
</code></pre>
<p>You can use <code>~</code> in the <code>--volume</code> flag because <code>~</code> is meaningful on your (Linux) host. The file is mapped to <code>/somewhere/.kube/config</code> in the container and so your Python file needs to point to the container location (!) when you invoke it.</p>
<p>Also, so that you may use command-line parameters, I encourage you to use <code>ENTRYPOINT</code> instead of <code>CMD</code> to run your Python program:</p>
<pre><code>...
ENTRYPOINT [&quot;python3&quot;,&quot;./App_pod.py&quot;]
</code></pre>
