I'm trying to containerize a python application, where I used the Kubernetes package. My Dockerfile is:
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 ["python3","./App_pod.py"]
when I enter docker run -i -t run, it shows me the following error:
> Traceback (most recent call last):
> File "//./App_pod.py", line 9, in <module>
> config.load_kube_config()
> File "/usr/local/lib/python3.10/site-packages/kubernetes/config/kube_config.py", line 813, in load_kube_config
> loader = _get_kube_config_loader(
> File "/usr/local/lib/python3.10/site-packages/kubernetes/config/kube_config.py", line 770, in _get_kube_config_loader
> raise ConfigException(
> kubernetes.config.config_exception.ConfigException: Invalid kube-config file. No configuration found.
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.
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?
A couple of issues:
~has a special meaning on Linux; it is a shortcut to the user's home directory. I think~is not available for the destination path of a DockerfileADD(orCOPY) command. You should use a specific path insteadconfig.ymlis the source butconfigis the destination which may be problematic.You don't include the full Dockerfile so it's unclear what your
FROMimage is and you don't include the source of yourApp_pod.pyfile so it's unclear where it looks for the kubeconfig file, but...runthe containerLet's assume you change your code to accept a command-line argument for
config.ymlin the working directory, e.g.:Then, when you run the container, you will need to mount the
config.ymlinto the container and reference it. In the following, I'm using different source and destination folders to demonstrate the point:You can use
~in the--volumeflag because~is meaningful on your (Linux) host. The file is mapped to/somewhere/.kube/configin the container and so your Python file needs to point to the container location (!) when you invoke it.Also, so that you may use command-line parameters, I encourage you to use
ENTRYPOINTinstead ofCMDto run your Python program: