Mount host directory with a symbolic link inside i

2019-02-01 16:32发布

I mounted the container with this parameter:

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

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

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
2条回答
Fickle 薄情
2楼-- · 2019-02-01 16:56

Symlink is a big challenge inside docker. In your case you can mount both directorires

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

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

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

查看更多
【Aperson】
3楼-- · 2019-02-01 17:07

One solution is to make Docker mount the original file, but use readlink -f which prints the file's actual location. This way, you can still reference the symlink location in your command, e.g.

docker run -it -v $(readlink -f /home/test/):/home/test/ ...

查看更多
登录 后发表回答