Mount host directory with a symbolic link inside in docker container

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


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


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.

Tags:

Docker