Docker for Mac volume mounts behave differently than the base Docker system. This is mostly because Docker tries to comply with Apple's filesystem sandbox guidelines.
As shown in Docker's preferences, only certain paths are exported by macOS.
/Users
/Volumes
/tmp
/private
/var
in macOS is a symbolic link into /private
. That is also true for /tmp
:
$ ls -ld /tmp /var
lrwxr-xr-x@ 1 root wheel 11 Jan 26 16:18 /tmp -> private/tmp
lrwxr-xr-x@ 1 root wheel 11 Jan 26 16:18 /var -> private/var
Why is /tmp
listed in the sharing panel, but /var
is not (even though both are a part of /private
)? Docker for Mac's documentation about filesystem namespaces explains:
By default, you can share files in /Users/
, /Volumes/
, /private/
, and /tmp
directly. To add or remove directory trees that are exported to Docker, use the File sharing tab in Docker preferences whale
menu -> Preferences -> File sharing. (See Preferences.)
All other paths used in -v
bind mounts are sourced from the Moby Linux VM running the Docker containers, so arguments such as -v /var/run/docker.sock:/var/run/docker.sock
should work as expected. If a macOS path is not shared and does not exist in the VM, an attempt to bind mount it will fail rather than create it in the VM. Paths that already exist in the VM and contain files are reserved by Docker and cannot be exported from macOS.
Note that /var/run
is specifically mentioned here as a place that would be mounted from the Linux VM, instead of from macOS.
When you ask for a volume mount, macOS filesystem exports are checked first. If there is no match there, the Linux VM where Docker is running is checked next. If neither of them have the path you requested, then the mount fails.
In your case, /var
is not exported by macOS. /var
exists in the Linux VM, but /var/folders
does not. Therefore, the path is not available, and the mount fails.
If you change the path to /private/var
, then it will succeed, because macOS exports the entire /private
filesystem tree for mounting.
In order to make things more portable, you may want to test which platform you are currently running on, and if it's macOS, prefix the mount path with /private
.