Skip to content
Advertisement

How do symlinks in a host volume work in Docker containers?

My question is about what happens when you create a symlink from inside a container in a host mounted directory. It’s easiest if I just ask with an example.

Assume we start a container like so. Most of the command doesn’t matter, what’s important here is the host volume mount.

docker run --rm -ti -h thecontainer -v /home/userguy:/container-home alpine /bin/sh

While in the container I create a symlink in the host volume directory

thecontainer$ ln -s /tmp /container-home/tmp-link

While in the container I can ls /container-home/tmp-link and I see the contents of the container’s /tmp as expected.

Now, if I go back over to my host machine, I see the link /home/userguy/tmp-link -> /tmp. If I ls that directory I see the contents of the host’s /tmp. i.e. Different results.

My question is, how do host volumes work under the covers that allow this situation to work? Is this a product of Docker or lxc itself? I was surprised to see this work because thought symlinks pointed to inodes and assumed /tmp in the container would be different from /tmp on the host.

Advertisement

Answer

It’s part of the virtual filesystem layer of the OS. A symlink contains a name, and the VFS resolves that name dynamically when the symlink is accessed.

Pointers to inodes are called links, usually called “hardlinks” to differentiate them from symlinks.

Advertisement