I use a simple Linux machine in Docker. Using it with Linux, I clone my dev repository and mount the repo as a volume in Docker. Then, when I enter into the Docker container, the files in the volume belong to user 1000 in group 1000 (all is fine because Docker keeps correctly all file owners). Now I'm trying to do the same in macOS, but in my macOS machine, my uid is 501 and my gid is 20. But when I go to the container, I realize that files inside it have gid and uid 0, the same as root. What can I do to keep file ownership in Docker?
相关问题
- Docker task in Azure devops won't accept "$(pw
- Unable to run mariadb when mount volume
- Unspecified error (0x80004005) while running a Doc
- Xcode debugger displays incorrect values for varia
- Is there a way to report errors in Apple documenta
TL;DR
The osxfs driver pretends that the files are owned by the
USER
that the container runs as. If you are seeing mounted files as being owned by root, your container probably is set to run as root.The longer version
The osxfs driver on macOS tells a bit of a lie about ownership. Here is the relevant section of the documentation (emphasis mine):
In other words,
Inside the container, the osxfs driver pretends that whatever uid/gid is running in the container is also the uid/gid that owns the mounted files.
If you chown files (in the container) to something else, no chown is performed on the real files; this owner information is stored in an extended file attribute instead, and that value is used by the container (and ignored by macOS).
The real files are owned by whoever owns them in macOS, outside the container. Access control is determined using those real file ownerships, and the uid/gid of the user running the Docker applications (which is probably your login user on the Mac).
I'll use a container on my Mac as an example. I built this container, and here is part of its Dockerfile:
As you can see, this container runs as the user "planner". As is typical in Ubuntu, being the first user added to this new system, it has a uid of 1000 and a gid of 1000.
Outside, on my Mac, my uid and gid are also typical (501, 20), but not the same as the "planner" user in my container.
I run this container with an external directory mounted, which contains my code. That's so I can reload the application during development without rebuilding the container (a common use case).
If I look at the source directory (
./uwsgi/planner
) on my Mac, the files are owned by me (uid 501, gid 20).But looking at the mounted directory inside the container, you can see that while the paths, dates, and sizes are the same, osxfs has masked the owner and told the container OS that these files are actually owned by "planner". The actual name and uid of the user are not important here. The osxfs driver is just using whatever the current user is. If you have a user with uid 1005 named "joe", then that is what you'll see instead.
Now, this is just the osxfs driver on Mac. And it is done in the name of simplicity - on a Mac you are probably doing development work, and the nuances of file permissions are just a pain you have to work around. Also, notice that you are not able to execute this kind of chown on your Mac without using sudo or another tool to elevate your privileges.
This matters, because aside from vmnetd, all of the Docker processes running on your Mac run as you, not as root. (That includes the osxfs process, as you can see in this screenshot.)
On a Linux server, it works more like you expect it to work. For example, as part of the same service I used in my example, I have a postgres container, and it uses an external directory for its data store. On my Mac, that directory is full of files owned by me (but the container thinks they are owned by user "postgres"). But, on the server, the real uids are preserved on the external filesystem.
Here the files are owned by uid 999, gid 999. That is not my uid (which is 5046 on this particular server).
999 is the uid of postgres inside the container, and that is exposed externally in the file permissions here.
In other words, the issue you are having is Mac-specific, and you should not have the same issue using Linux in production.
why do you need to do this?
if you're using a host-mounted volume (via
docker run
, with-v $PWD:/my/app
args for example) in your container, then it doesn't matter what the permissions are in the image itself.you can edit the files on your mac and the changes will be immediately reflected in the container.
this is all i ever do from my mac, and i never have issues editing files in my volume or having those changes reflected in the container