python FileNotFoundError: [Errno 2] with absolute path and file exists

7/8/2018

I'm running a Python script as a Kubernetes job. But I'm having the next issue. I've already tried with chmod -R 777 /home/vagrant/ but it doesn't make the script run successfully.

Traceback (most recent call last):
  File "app.py", line 39, in <module>
    main()
  File "app.py", line 28, in main
    config.load_kube_config(config_file=kubeconfig)
  File "/usr/local/lib/python3.6/site-packages/kubernetes/config/kube_config.py", line 470, in load_kube_config
    config_persister=config_persister)
  File "/usr/local/lib/python3.6/site-packages/kubernetes/config/kube_config.py", line 427, in _get_kube_config_loader_for_yaml_file
    with open(filename) as f:
FileNotFoundError: [Errno 2] No such file or directory: '/home/vagrant/.kube/config'

As you can see the script fails supousedly because such file doesn't exists, but when I do an ls the file shows that it does exists.

vagrant@vagrant:/vagrant/podsLister$ kubectl delete job pod-lister
job.batch "pod-lister" deleted
vagrant@vagrant:/vagrant/podsLister$ ls /home/vagrant/.kube/
cache  config  http-cache
-- Mariano Rodríguez
kubernetes
minikube
python
python-3.x

1 Answer

7/9/2018

The problem is that the script is running inside a Docker container. That container has its own filesystem, not the filesystem of whatever machine happens to be hosting it. That is, in fact, most of the point of containers: they act like an isolated, separate computer, without you needing an actual separate computer.

The solution is a little less obvious, because it's not clear why you expected this to work, or what you're trying to do. Things that might make sense include:

  • Changing your docker build script to copy the file into the container.
  • Setting up host file sharing, so part of the real machine's filesystem is available to containers, then making the container mount that.
  • Setting up "normal" file sharing, so part of the real machine's filesystem is available to anyone on the network, then making the container mount that.
  • Using some higher-level feature like Kubernetes ConfigMaps to expose the file to containers.
-- abarnert
Source: StackOverflow