在 docker 容器中挂载带有符号链接的主机目录

问题描述:

我用这个参数挂载了容器:

I mounted the container with this parameter:

-v/home/test/:/home/test

-v /home/test/:/home/test

在宿主机的/home/test 中,有一个符号链接指向/mnt/文件夹.

Inside /home/test in the host there is a symbolic link pointing to a /mnt/ folder.

但是该链接虽然可以看到指向的位置,但在容器内部似乎已损坏:

But that link, although can be seen where is pointing to, seems broken inside the container:

root@f93f72b45013:/var/www/html# cd /home/test/ 
root@f93f72b45013:/home/test# ls -lrt 
total 11956 
lrwxrwxrwx. 1 root root 40 Jul 20 15:55 file -> /mnt/mountedfile/
root@f93f72b45013:/home/test# ls -lrt file/*
ls: cannot access file/*: No such file or directory

这甚至可以在 docker 中完成吗?我不确定是否有办法做到这一点.

Is that even possible to be done in docker? I am not sure if is there a way to do it.

我知道我可以直接挂载符号链接指向的位置,但我只是想知道这是否可行.

I know I can just directly mount where the symbolic link is pointing at but I was just wondering if this is possibe.

符号链接是 Docker 中的一大挑战.在您的情况下,您可以挂载两个目录:

Symlinks are a big challenge inside docker. In your case you can mount both directories:

-v /home/test/:/home/test -v /mnt/mountedfile:/mnt/mountedfile

要使符号链接在容器内外都能工作,它们必须是绝对路径并使用完全相同的名称.

For symbolic links to work both inside and outside the container, they have to be absolute paths and use exactly the same names.

通常,符号链接在 docker 中不起作用.我发现这很难.

In general, symlinks do not work inside docker. I found this the hard way.