2

I've done some searching, and it looks like symlink is not supported inside a docker container, is there a workaround? I'm also using a volume that mounts to my home directory (on the host), and this is persistent.

When I load the container, existing symlinks in my home directory don't work. I've even tried creating symlinks inside the container, that doesn't work either.

EDIT: I was playing around with mounting >1 volumes, and may have found something. If I mount a volume that mirrors the symlink path, then it seems to work. For example, if I have a data folder that symlinks to /media/disk1 on my /work directory inside the container (and /work is mapped to /home on the host), I can mount another volume that attaches /media/disk1 to /work/data, (and /media/disk1 is symlink to /home/data on the host), then symlink works inside the container!

xnet
  • 381
  • 4
  • 11
  • 4
    The sweeping statement *"symlink is not supported inside a docker container"* Is just not true. You clearly have a problem though, so please show what you are *actually* trying, and what happens. – SiHa Mar 26 '19 at 16:47
  • 1
    A proper [mcve] *includes everything* (albeit the shortest/smallest possible "everything") *someone else needs to reproduce the problem you're asking about*. As SiHa says, symlinks in Docker container *are* supported -- so this question is, as currently written, stating an untrue premise rather than showing how to reproduce a narrow and specific (and presumably very real) problem. – Charles Duffy Mar 26 '19 at 16:49
  • I'm not using my words, but many have reported symlinks are not "fully supported", e.g.: https://stackoverflow.com/questions/31881904/docker-follow-symlink-outside-context – xnet Mar 26 '19 at 17:05

2 Answers2

9

I'll answer my own question, in case someone else comes across this issues.

First, we create volume

docker volume create --name work type=none --opt device=/home/username --opt o=bind

Then, we run the container

docker container run -it --rm --mount source=work,target=/work

That way my host home directory (/home/username) gets mapped to my /work directory in the container.

Now, in my home directory, there is a symlink data -> /media/disk1/data which does not work inside the container.

However, if I setup another volume and mount such that the path matches the symlink, i.e.

docker volume create --name data type=none --opt device=/media/disk1/data --opt o=bind
docker container run -it --rm --mount source=work,target=/work --mount source=data,target=/work/data

Then, the symlink works.

I believe the issue is that the symlink reference a path which the container is "unaware" of, and only by mounting that path as an additional volume in the right place, did the symlink work.

xnet
  • 381
  • 4
  • 11
0

The symlink needs to be created relative to your container, not your local machine. Access the command line in your container with:

docker exec -it name_of_your_php_container bash

Your symlink should be similar to:

storage -> /var/www/html/storage/app/public

It should NOT be relative to your local machine, i.e.

storage -> /Users/name/Sites/my-project/storage/app/public
Jon Winstanley
  • 21,816
  • 20
  • 70
  • 107