Skip to content
Advertisement

docker container can’t open symlinked file in volume mount during startup

I have a docker container which is launched with docker-compose.yml which contains

service: my-service
  volumes:
  - /tmp/mount/opt/my-app:/opt/my-app

There are symlinks in /tmp/mount/opt e.g. /tmp/mount/opt/my-app/test1.cfg which points to /host/path/test1.cfg

When the container starts up, it fails with an error like:

my-service | Failed to stat config file "/opt/my-app/test1.cfg": No such file or directory

However if I docker exec -ti the_same_container bash and cat /opt/my-app/test1.cfg then I see exactly the expected contents.

Also if on the host I run cp -r -L /tmp/mount /tmp/mount_copy and modify the yml

service: my-service
  volumes:
  - /tmp/mount_copy/opt/my-app:/opt/my-app

then everything works.

In other words, the symlink in the mounted directory can be read by the container after it starts up, but not during, and mounting a non-symlinked clone of the directory is fine.

Why are symlinks behaving like this in docker?

Advertisement

Answer

A symlink points to a file in the file system. So the symbolic link in the docker host’s file system points to a file in that file system.

If you mount that symbolic link into the container it is interpreted within file system of the container. This means that it still points to /opt/my-app/test1.cfg. And this file does not exist in the container.

You can mount the original directory, can’t you?

- /opt/my-app/:/opt/my-app

I guess you don’t want to mount the whole directory. You might want to exclude some files and that’s why you created a temp directory with symbolic links.

In this case it might be possible to create the temp directory and use hard links, because they are just another file entry which point to the same files. But this is only possible if the hard link is created on the same partition. See What is the difference between a hard link and a symbolic link

Maybe docker’s bind-propagation capapilities can help you too.

Why are symlinks behaving like this in docker?

It is not a docker issue. Symbolic links just behave that way.

Also take a look at Mount host directory with a symbolic link inside in docker container

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement