Search code examples
dockerdocker-composemounted-volumes

Reflect changes in read-only docker volume to the container?


I have a config file being injected into Docker at runtime using a read-only volume. Thus the container cannot change the contents of the config file, but the host can. The idea is to send a SIGHUB to the process inside the container to reload any config changes. However, Docker seems unable to detect any changes to the config file, and seems to see a frozen snapshot of the file as-of the time when the container started.

Is this expected behaviour for Docker?


Solution

  • A bind mount, which docker uses for host volumes, is mapping an inode to a second location. An inode is a pointer to a file or directory in Linux. And the standard process for most editors is not to modify a file in place, but to create a temporary file with a new inode, and then replace the existing file with one that has a new inode. This avoids corruption where a crash would leave the file in a partially written state. You should see this happening with an ls -i on the file.

    For a bind mount, this means you still have the bind mount to the original inode even after the host sees the file with new content. You can solve that by either:

    • bind mount the entire folder instead of only the file
    • recreate the container to pickup the new change (a restart may work too)
    • edit the file without changing the inode

    For the latter, commands like echo new line >>file or cat tmp-file >file will keep the same inode on file. You may find settings for your editor too.