Search code examples
linuxdockerubuntu-14.04volume

Sharing a single file from host machine with Docker Container and having the Container r+w to same file


I've got a situation where I've got a .json file that I want to persist between runs of a given container. In addition this file needs to be appended to by the container as part of it running.

The syntax which I've been using so far is as follows:

docker run -d -p 3001:3001 -v /usr/bob/database.json:/app/data/database.json:rw --name=myapp appImage  

Nothing gets inserted into the file (though I can cat the contents inside and outside the container to confirm it's the same). I have ensured that the root user (yes not best practice) who is running docker owns all of the files in that folder and has full rwx.

What DOES work is if I bind at the folder level eg:

docker run -d -p 3001:3001 -v /usr/bob:/app/data --name=myapp appImage

Can anyone explain the difference?

I feel that sharing access to a folder instead of a single file is a lot less precise and also causes structural changes in the app (eg. source control with multiple files (plus the .json file mentioned) in the same folder).

Thanks in advance for any pointers.

Thanks,

Andrew


Solution

  • Mounting a file as a volume mounts a specific inode inside the container. Many tools that modify a file will change the inode when writing a new copy of the file. This new inode will be stored in the directory as the new pointer to that filename. When they directory is mounted you see the change in your host, but otherwise you only see it inside the container since the inode on the host and the pointer to it in the host directory are unchanged.

    There are more details on this behavior in Docker's tutorial on volumes: https://docs.docker.com/engine/tutorials/dockervolumes