Using Docker for Windows with Linux containers, it appears that volumes are always mounted as root inside containers.
Trying to change either the owner (with chown) or the permissions (with chmod) doesn’t return any error but nothing changes.
Maybe this is a well know thing but I can’t find exact information on this subject anywhere. How does these permissions work?
On a Mac for example, things are a little different. Volumes get mounted as root but the owner can be changed using chown. chmod on a Mac sometimes succeeds, sometimes it returns “Operation not permitted”.
A use case is when trying to use Apache with a mounted volume as document root. It won’t work because all files are owned by root.
A quick replication example (in PowerShell):
## Prepare:
$ cd ~; mkdir test-owner-dockr; cd test-owner-dockr
$ echo "Testing ownership and permissions" > README.md
$ docker run -d --name testowner -v "$(pwd):/tmp/win" -w /tmp/win busybox sh -c 'while [ true ]; do date; sleep 1; done'
## View original:
$ docker exec testowner ls -l
total 1
-rwxr-xr-x 1 root root 72 Jan 5 15:51 README.md
## Change permission (nothing happens, command succeeds):
$ docker exec testowner sh -c 'chmod 0777 README.md && ls -l'
total 1
-rwxr-xr-x 1 root root 72 Jan 5 15:51 README.md
## Change ownership (nothing happens, command succeeds):
$ docker exec testowner sh -c 'chown nobody README.md && ls -l'
total 1
-rwxr-xr-x 1 root root 72 Jan 5 15:51 README.md