I have a docker container with a -v /home/dan:/home/dan:rw
. When the container writes files to /home/dan
, the files are owned by root in the host filesystem. Is there a way to make it so that files written from the container to the mounted volume are owned by some arbitrary user on the host filesystem?
RUN useradd --create-home appuser USER appuser RUN mkdir /home/appuser/my_volume ... Now appuser has write permissions to the volume as it's in their home directory.
Manage Docker as a non-root userThe Docker daemon always runs as the root user. If you don't want to preface the docker command with sudo , create a Unix group called docker and add users to it.
Installing software is something only the root user can do. So container build systems set you up, by default, with the root user. Unless you explicitly add a non-privileged user, the containerized software will continue to be set to keep running as root.
As François Zaninotto has pointed out, the user id can be used.
This can be done by using the -u switch for the docker run command.
For example:
docker run -v /home/dan:/home/dan -u `id -u $USER` IMAGE
EDIT: this has changed since my original answer which said it couldn't be done. As per answer of Mandark:
This can be done by using the -u switch for the docker run command.
For example:
docker run -v /home/dan:/home/dan -u `id -u $USER` IMAGE
A follow up to mandark answer - I would say it's also good to include the user group otherwise you will end up with stuff belonging to user: USER
and group: root
. To achive user:user
just pass in group id as well, for example:
docker run -v /home/dan:/home/dan -u `id -u $USER`:`id -g $USER` IMAGE
# if it's for the current user, then you can omit the $USER env var
docker run -v /home/dan:/home/dan -u `id -u`:`id -g` IMAGE
It's possible. It's hard to automate, but it's possible. Here is the process:
in the docker container, run a shell script to:
Now, each file generated inside the container will be using the right user id and group id, and the host will attach them to your user.
I've written a tool to automate that using make, it's called make-docker-command. Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With