-
Notifications
You must be signed in to change notification settings - Fork 18.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add ability to mount volume as user other than root #2259
Comments
If you
(Assuming that Another possibility is to do the bind-mount, then |
@mingfang Will chown not work for you ? |
It would be useful to have a shortcut for this. I often find myself writing https://github.com/orchardup/docker-redis/blob/07b65befbd69d9118e6c089e8616d48fe76232fd/run |
What if you don't have the rights to |
Would a helper script that |
Can I say no - forcing users to add a helper script that does
(thanks @bfirsh for your eg) is pretty terrible. It means that the container has to be started as root, rather than running as the intended and it means a user can't do something like:
|
There is one way to make it work, but you need to prepare ahead of time inside your Dockrfile.
(I didn't test this example, I'm working on a chromium container that then displays on a separate X11 container that .... ) |
And of course that method only works for direct new volumes, not bind |
Additionally, multiple containers using |
@SvenDowideit @tianon that method doesn't work either. Full example:
Two runs, with and without a -v volume:
|
We're hitting an issue that would be solved by this (I think). We have an NFS share for our developer's home directories. Developers want to mount This forbids root from accessing |
@frankamp This is because docker's current preference is to not modify host things which are not within Docker's own control. Your "VOLUME" definition is being overwritten by your I'm not sure there is much that can be done here, and unfortunately bind mounts do not support (as far as I can tell) mounting as a different uid/gid. |
My solution to this was to do what SvenDowideit did above (create new user and chown up front in dockerfile), but then instead of mounting the host volume, use a data-only container, and copy the host volume I wanted to mount into the container with |
What about acl? |
Is there any fix or workaround? I run into same issue with OpenShift, mounted folder is owned by root:root and precreated images wont work. |
I'm looking for a workaround too. If all mounted volumes are owned by |
Well you can try s6-overlay. It includes features which are specifically targeted to help to work-around these kinds of problems. |
@dreamcat4: Thanks for the pointer. The fixing ownership & permissions seems like an interesting workaround, but wouldn't I have to run my Docker container as root for that to work? |
@brikis98 Yes that is true. However s6-overlay also has yet another feature, which allows you to drop the permissions back again when launching your servers / daemons. |
@dreamcat4 Ah, gotcha, thanks. |
Its also problematic when you use secrets that should be accessible by specific user. For example And that hack to know the |
To my mind it would be ideally: |
As noted in the limitations section of tmpfs mount docs (this was added to docs in Feb 2023), you can set the UID/GID of what you'd expect it to be instead of the default # Example
$ docker run --rm -it --tmpfs "/data:exec,uid=$(id -u),gid=$(id -g),mode=0644,size=1G" alpine ash
$ apk add exa
# Mode is correctly changed, so is the UID and GID:
$ exa --octal-permissions -ldgh /data
Octal Permissions Size User Group Date Modified Name
0644 drw-r--r-- - 1000 1000 28 Sep 23:03 /data
# Size is correctly applied:
$ df -h /data
tmpfs 1.0G 0 1.0G 0% /data This PR that didn't get merged additionally notes:
Not exactly, size is 50% of Docker host by default: $ docker run --rm -it --tmpfs "/data" alpine ash
$ mount | grep /data
tmpfs on /data type tmpfs (rw,nosuid,nodev,noexec,relatime)
$ cat /proc/mounts | grep /data
tmpfs /data tmpfs rw,nosuid,nodev,noexec,relatime 0 0
$ df -h /data
Filesystem Size Used Available Use% Mounted on
tmpfs 7.6G 0 7.6G 0% /data Additionally the Docker docs on tmpfs incorrectly state:
Clearly it does support options now as shown above. |
it's a volume, not a mount because I wasn't confortable using the user:root and didn't want to go deeper in sysadmin stuff can read more about difference between --mount and -v here: https://docs.docker.com/storage/volumes/#choose-the--v-or---mount-flag rabbit hole here: moby/moby#2259
## Description: This allows users to change the user & group with which a container starts ## Is this change user facing? YES ## References (if applicable): Closes #2000 Note this is a workaround to solve #2000 while moby/moby#2259 remains open
Executing my springboot project as a non-root?I've been trying to run a Java JAR launcher as a non-root user for my Spring Boot project. I've already added the user and set the working directory accordingly. However, the project still seems to be running with root privileges. Any suggestions on the best approach to ensure it runs as the designated user? |
Reports of problems with write-access to volumes on the host with Docker, in particular in rootless mode and with a non-root container user, are all over the WWW. Because this was a hard blocker for my work, I took the time to analyze it in depth; for my findings and a 2024 solution, see |
hit the same issue while using userns-remap mode. |
Having the work directory at a fixed known location in the environment is a bit more convenient in many cases. There's also an obscure reason why it helps Docker volume mounts. When you do a Docker volume mount on a non-existing directory, it's owned by root. When you do a Docker volume mount on a directory that exists in the image, it takes on that directory's ownership. See <moby/moby#2259>. It's hard to make the directory exist in the image when you can't predict its name, so this change will help.
Here's a workaround project I built if anyone's interested: Docker User Mirror It's a pair of shell scripts that:
$ id
uid=1000(user) gid=1000(user) groups=1000(user)...
$ cat image/Dockerfile
FROM debian:latest
COPY entrypoint /entrypoint
ARG GOSU_VERSION=1.17
ARG SETPRIV_VERSION=2.40.1
ARG UTIL_LINUX_VERSION=2.39.3
RUN chmod +x /entrypoint && /entrypoint --setup
ENTRYPOINT ["/entrypoint", "--"]
$ docker build -q image/
sha256:db48660e27...
$ ./user-mirror docker run -it --rm -v test_volume:/mnt/test_volume db48660e27 sh
$ id
uid=1000(user) gid=1000(user) groups=1000(user)
$ touch /mnt/test_volume/test_file
$ ls -l /mnt/test_volume/test_file
-rw-r--r-- 1 user user 0 Oct 1 05:46 /mnt/test_volume/test_file Also works with compose: $ docker compose build
$ ./user-mirror docker compose run --rm {service} |
Addresses #15663. This PR saves the entire coder home directory in a Docker volume to make the dev tunnel URL persistent across container restarts. I initially wanted to persist only the config directory, but Docker Compose cannot set permissions on a named volume unless the directory it’s mounted on already exists within the container. The `/home/coder/.config` directory, however, is not created by default in the Dockerfile. When I attempt to mount it, [Docker creates it with root permissions](moby/moby#2259 (comment)), and Coder cannot write to it. I encounter the following error: ``` coder-1 | Started HTTP listener at http://0.0.0.0:7080 coder-1 | Opening tunnel so workspaces can connect to your deployment. For production scenarios, specify an external access URL coder-1 | Encountered an error running "coder server", see "coder server --help" for more information coder-1 | error: create tunnel: read or generate config: get config path: mkdirall config dir "/home/coder/.config/coderv2": mkdir /home/coder/.config/coderv2: permission denied ``` Creating the directory in the Dockerfile would resolve the issue for new images but would break `docker-compose.yml` for all existing Coder images. Mounting the entire home directory avoids this problem, but it makes it less clear to admins which files need to be persisted. It’s a trade-off - I believe keeping Docker Compose backwards-compatible is more important, and I hope the added comment clarifies the purpose of the volume for new users.
I'm looking for a workaround too. If all mounted volumes are owned by root, it makes it impossible to run your Docker containers with any user other than root |
Use case: mount a volume from host to container for use by apache as www user.
The problem is currently all mounts are mounted as root inside the container.
For example, this command
docker run -v /tmp:/var/www ubuntu stat -c "%U %G" /var/www
will print "root root"
I need to mount it as user www inside the container.
The text was updated successfully, but these errors were encountered: