I am running Docker in Ubuntu. I have a container with the following folders: /opt/foo
and /opt/bar
. I have a file in /opt/bar
called data.txt
with 1GB which is hardlinked into /opt/foo
.
My question is: what happens if I have /opt
as a volume in my container mapped to a folder on my host? (such as -v /home/me/my_opt:/opt
) Will my file also exist in my host as a hardlink on both places, effectively only using up 1 GB? Will my host instead break the hardlink and have the file copied into both places, effectively taking up 2 GBs? Will something else happen?
When you have a bind mount, nothing is copied. The same filesystem exists in one path on the host and on a path in the container. If you already understand Unix hard links, in fact, it’s extremely similar, except that it applies to whole directory trees instead of individual files.
So in your example, there is only a single 1 GB file. The same physical file is referenced by /opt/foo/data.txt
and /opt/bar/data.txt
on the host, and /home/me/my_opt/foo/data.txt
and /home/me/my_opt/foo/bar/data.txt
in the container.
This is more specifically true directly using the Docker engine on native Linux. If you’re using Docker Desktop, or another solution based on a virtual machine, there will need to be some mechanism to sync files between the host and the VM. These to my knowledge don’t usually cache or persist files, and you shouldn’t see a difference if a file is hard-linked or not, but it is a little harder to make direct statements about what happens.