Mounting symbolic links inside directories

Hi, I have a question about bind mounts and symlinks. When I try to bind mount a symlink by providing the path to the file, Docker will “follow” the link first, so the container can see it just fine. However, when I provide the path to a directory that contains symlinks inside it won’t do this, and the container only sees the symbolic links which of course don’t point to anything at that point.

My use case is for an nginx reverse proxy running in Docker Swarm, although I don’t think the Swarm part is relevant here. As I deploy and remove services, I want the reverse proxy to be aware of any changes via symlinks inside it’s own sites-enabled directory, which I mount to the container like so:

  - ./sites-enabled:/etc/nginx/sites-enabled

But as mentioned the symlinks inside this directory are not resolved. To make that work I would have to point to each site’s configuration individually, like so:

  - ./sites-enabled/site1.conf:/etc/nginx/sites-enabled/site1.conf
  - ./sites-enabled/site2.conf:/etc/nginx/sites-enabled/site2.conf
  - ./sites-enabled/site3.conf:/etc/nginx/sites-enabled/site3.conf
  # ...

For now my solution is fairly simple although not entirely ideal: copy the config file instead of creating a symbolic link. Is there a way to tell Docker that it should resolve symlinks before mounting a directory?


Well, in case of containers we don’t really use virtual hosts/sites. We run each site in a separate container. Since you mentioned that the nginx is a reverse proxy, you shouldn’t have different files and you should definitely not create the files manually. It should be automated. For example


Just to answer your question, you could mount the folder that contains the actual files too or just mount the folder that contains the files instead of mounting the folder of links.

1 Like

Hi, thanks for the help. I think you are right, my first thought was to copy the files and mount those instead, but I understand why you’d recommend against this… I’ve decided that is probably best to use Docker Config since I’m running on Swarm. This makes things cleaner and much easier to work with both on Docker and Ansible.

I really like that nginx-proxy project! This is what I was trying to do myself but run into all sorts of issues whenever I needed to deploy or destroy containers. Thanks again, I will check it out.

If you like Nginx Proxy, you should check Traefik as well :slight_smile:

I used Nginx Proxy for years (not in swarm mode) but switched to traefik.

I’ve had Traefik in my list of things to learn for some time and I’ll check it out soon. I just wanted to make this setup on my own to learn and understand why things like Traefik exist in the first place. It looks like nginx-proxy is designed to work with containers alone, not so much with services? I think it’s worth a try anyway. Cheers!

Could be true. Nginx-proxy needs the docker socket but the docker socket is to communicate with the local Docker daemon and watching events. It can detect when a container starts or terminates so inspecting the container’s metadata it can create the nginx configuration. I haven’t used a reverse proxy in swarm, only in Kubernetes and Docker without Swarm. Each system can have different way for watching events.