micromamba-docker: mambauser cannot write to bind mounts on Linux with Docker in rootless-mode
If a script wants to write to the current working directory on the host system, an obvious way is to use a bind mount to map a directory on the host to a directory inside the container.
Unfortunately, this does not work with Docker on Linux systems; the non-root mambauser cannot write to directories from bind mounts, no matter if we set the UID/GID to that of the user on the host or not:
# Minimal example (works on Docker Desktop on OSX)
$ docker run --rm -it -v "$(pwd):/tmp" \
mambaorg/micromamba:1.5.6 /bin/bash
$ id
uid=57439(mambauser) gid=57439(mambauser) groups=57439(mambauser)
$ touch test.txt
touch: cannot touch 'test.txt': Permission denied
$ touch /tmp/test.txt
touch: cannot touch '/tmp/test.txt': Permission denied
# With using the host user's UID and GID
$ docker run --rm -it --user $UID:$GID -v "$(pwd):/home/mambauser" \
mambaorg/micromamba:1.5.6 /bin/bash
$ cd /home/mambauser/
$ echo test > test.md
bash: test.md: Permission denied
$ ls -la
total 8
drwxr-xr-x 2 root root 4096 Jan 11 03:15 .
drwxrwxrwx 3 root root 4096 Dec 30 15:30 ..
$ pwd
/home/mambauser
Writing to Docker bind volumes on Linux systems as non-root users is a well-known and complicated topic, but I wonder if there is an elegant way of adding the mambauser to the group that has write access to a bind mount point.
Or is there any other way of writing to a directory on the host from the mambauser?
Note: The issue does not appear on Docker Desktop for OSX, as the built-in VM maps between the host system and the Docker environment.
References:
- https://jtreminio.com/blog/running-docker-containers-as-current-host-user/#ok-so-what-actually-works
- https://stackoverflow.com/questions/39397548/how-to-give-non-root-user-in-docker-container-access-to-a-volume-mounted-on-the
- https://stackoverflow.com/questions/56019914/docker-user-cannot-write-to-mounted-folder
- https://github.com/moby/moby/issues/2259
- https://www.joyfulbikeshedding.com/blog/2021-03-15-docker-and-the-host-filesystem-owner-matching-problem.html
Addendum: Tested with Micromamba:1.5.6, Docker version 24.0.7, build afdd53b on Debian 11.8 Docker version 24.0.7, build afdd53b
About this issue
- Original URL
- State: open
- Created 6 months ago
- Comments: 17 (3 by maintainers)
Solution and Conclusion
1. The best solution is to install
Podman, which is super-simple, and to use the--userns=keep-idoption. As the additional uid/gid parameters described in the current version are not available in e.g. Podman 3.0.1, you best use this command:In there,
touch testfile.txtworks like a charm, and the external username and id is also visible from inside.2. The second-best solution is to use
--user rootwhen starting the container if and only if Docker is running in rootless mode and if the host user has no sudo rights.**. This can be a quick fix with acceptable risks in most cases.touch testfile.txtworks, too, and the container root user is mapped to the local user on the host system.The other options do not work; tweaking the
subuidandsubgidmapping does not work without brittle and intransparent modifications on the host system.Hope this is useful for many of you! Frankly, it was a nightmare to get to this solution and insight and I learned many things that are intellectually interesting but were not on my bucket-list to master 😉.
Most important to say:
micromamba-dockeris not all all to blame that it was such a painful journey; it is Docker’s insufficient documentation and the lack of--userns=hostand/or--userns=keep-idsupport; deeply hidden in Docker issues and other sources. The hundreds of related posts and discussions indicates that many, many others are wasting their life-time with this Docker problem. So please spread the word.TODO: Add summary and pointer to this to the documentation.
@mfhepp thanks for the detailed investigation and clear recommendations. I am certainly in support of increasing our documentation in this area. Do you have any interest in putting together a documentation PR?
Wow, thanks @mfhepp for your persistence in getting to the bottom of this issue! This looks like it was quite some effort.
I have not yet used rootless mode myself (although I probably should), so this is a bit beyond my current comfort level. Thus I don’t have a preference among your suggested approaches for a fix. Perhaps @wholtz has some thoughts?
Still working on the issue… and I think there is an underlying problem that surfaces when you are using
micromamba-dockerAs far as I understand, the non-root
mambauserconflicts with the way Docker in rootless mode deals with bind mounts and namespace mapping in general.Docker in rootless mode utilizes
rootlesskit, which maps user namespaces between the host system and the system inside the container.See how
rootlesskitchanges the username, UID, and GID of resources owned by a local user:You can see that
rootlesskitchanges the owning user and user group frommyusernametorootand maps the UID from 1000 to 0.This means that the non-root
mambausercannot access such files and folders, even if the UID/GID of the local user is passed to the container when Docker is run in the rootless mode (for which there are good reasons).The exact mapping is determined by
/etc/subuidand/etc/subgid.I am reopening the issue, because
It may be possible to fix this by
/etc/subuidand/etc/subgid, or modifying_entrypoint.shshell script orDockerfile.Any ideas will be very much appreciated!
PS: The test from earlier on fails now, too, since I changed my system to a rootless Docker configuration:
As shown above with
rootlesskitalone, despite the indicated GID and UID being identical, the user namespace mechanism changes the owner torootfrom inside the container: