Mount host directory with a symbolic link inside in docker container

Docker

Docker Problem Overview


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 Solutions


Solution 1 - 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.

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

Solution 2 - Docker

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/ ...

Solution 3 - Docker

Hello thanks for your help In my case I was struggling activating https on my angular nginx app

docker run -p 80:80 -p 443:443 \
   --name test \
   -v /etc/letsencrypt/live/exemple.com:/etc/nginx/certs \
   -v /home/admin/nginx-default.conf:/etc/nginx/conf.d/default.conf:ro test

Does not mount links cert.pem and privkey.pem inside docker

But if I use all file path explicitly like that

docker run -p 80:80 -p 443:443 \
   --name test \
   -v /etc/letsencrypt/live/example.com/cert.pem:/etc/nginx/certs/cert.pem \
   -v /etc/letsencrypt/live/example.com/privkey.pem:/etc/nginx/certs/privkey.pem \
   -v /home/admin/nginx-default.conf:/etc/nginx/conf.d/default.conf:ro test

Everything worked I suppose you could do it exactly the same thing with docker-compose

Solution 4 - Docker

I can´t reply the previows answer but on a similar scenario where I need to get a linked file, setting up complete file path as you suggested on compose file worked out for me:

  - /opt/cert/ssl/live/example.com/cert.pem:/certs/cert.pem
  - /opt/cert/ssl/live/example.com/privkey.pem:/certs/privkey.pem

Files cert.pem and privkey.pem under example.com are both links to the files where certbot locates them.

Solution 5 - Docker

I think a good method to this issue.

In docker container, a symlink mounted from host cannot work properly. But a symlink created inside docker container work just fine. So, it is a good idea to mount the root (with absolute path) of interest into container first and then create symlink inside container with structures that satisfies ones' need. In this way, you are good to go.

Hope this helps.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionlapinkoiraView Question on Stackoverflow
Solution 1 - DockerKemin ZhouView Answer on Stackoverflow
Solution 2 - Dockeruser2640621View Answer on Stackoverflow
Solution 3 - DockerScorprocSView Answer on Stackoverflow
Solution 4 - DockerAgustin HerreraView Answer on Stackoverflow
Solution 5 - DockerDerek SHIView Answer on Stackoverflow