Search code examples
pythondockerfilesystemsdocker-volumeprometheus-node-exporter

Is it possible to get a Docker container to read a file from host file system not using Volumes?


I have a Python script inside a container that needs to continuously read changing values inside a file located on the host file system. Using a volume to mount the file won't work because that only captures a snapshot of the values in the file at that moment. I know it's possible since the node_exporter container is able to read files on the host filesystem using custom methods in Golang. Does anyone know a general method to accomplish this?


Solution

  • I have a Python script [...] that needs to continuously read changing values inside a file located on the host file system.

    Just run it. Most Linux systems have Python preinstalled. You don't need Docker here. You can use tools like Python virtual environments if your application has Python library dependencies that need to be installed.

    Is it possible to get a Docker container to read a file from host file system not using Volumes?

    You need some kind of mount, perhaps a bind mount; docker run -v /host/path:/container/path image-name. Make sure to not overwrite the application's code in the image when you do this, since the mount will completely hide anything in the underlying image.

    Without a bind mount, you can't access the host filesystem at all. This filesystem isolation is a key feature of Docker.

    ...the [Prometheus] node_exporter container...

    Reading from the GitHub link in the question, "It's not recommended to deploy it as a Docker container because it requires access to the host system." The docker run example there uses a bind mount to access the entire host filesystem, circumventing Docker's filesystem isolation.