I have a directory /home/foo/test owned by foo:foo (uid=1000, gid=1000) on the host that I bind mount in the node Docker image as such:
$ docker run --rm --mount type=bind,source=/home/foo/test,target=/home/node node:14-bullseye ls -l /home/node/README.md
-rw-rw-r-- 1 node node 3196 Sep 7 15:13 /home/node/README.md
As you can see the mounted directory is owned by the same uid:gid as the host machine. This is what I expect.
The uid:gid of the node user in the container is also 1000:1000:
$ docker run --rm --user node node:14-bullseye id
uid=1000(node) gid=1000(node) groups=1000(node)
But when I run the exact same command on another computer, the mounted directory and itās contents are owned by root:root:
$ docker run --rm --mount type=bind,source=/home/foo/test,target=/home/node node:14-bullseye ls -l /home/node/README.md
-rw-rw-r-- 1 root root 3196 Sep 7 15:13 /home/node/README.md
I thought Docker passed through file ownership when bind mounting in a container? Why does it work on one machine, and not the other? Is there any way to retain the ownership (i.e. have /home/node owned by 1000:1000 in the container)?
Both machines run Ubuntu Jammy with the same Docker versions:
You can run docker context ls to see if you are using the rootless context or just run docker info to get the current context name and docker data root which is different for rootless Docker.
$ docker context ls
NAME DESCRIPTION DOCKER ENDPOINT KUBERNETES ENDPOINT ORCHESTRATOR
default * Current DOCKER_HOST based configuration unix:///var/run/docker.sock swarm
It is not rootless. It is Docker CE and Docker Desktop. On the second machine you installed Docker Desktop which runs a virtual machine and your containers will run inside that virtual machine. In that case you canāt just mount the host folder the same way into the containers, because you need to mount it first into the virtual machine. Although I tried Docker Desktop for Linux, I donāt remember how mounting works, because I usually use Docker Desktop for Mac or just Docker CE. On the second machine, if you have Docker CE installed too, you can switch back to the ādefaultā context.
It is not rootless. It is Docker CE and Docker Desktop. On the second machine you installed Docker Desktop which runs a virtual machine and your containers will run inside that virtual machine.
It will install a virtual machine even when installed on GNU/Linux? I didnāt know that.
We completely removed Docker from the second machine and installed just Docker Engine, and now mounts work as expected. Thanks for the help!
Yes! This was exactly the issue using Ubuntu as a host, installing Docker Desktop and not understanding why Docker keeps setting bind-mounted folders and their contents to be user- and group-owned by root. Using the ādefaultā context with docker context use default switched back to Docker CE and restored the expected behavior of files retaining the ownership defined on the host.