I am running my application in a Docker container as a non-root user. I did this since it is one of the best practices. However, while running the container I mount a host volume to it -v /some/folder:/some/folder
. I am doing this because my application running inside the docker container needs to write files to the mounted host folder. But since I am running my application as a non-root user, it doesn't have permission to write to that folder
Question
Is it possible to give a nonroot user in a docker container access to the hosted volume?
If not, is my only option to run the process in docker container as root?
There's no magic solution here: permissions inside docker are managed the same as permissions without docker. You need to run the appropriate
chown
andchmod
commands to change the permissions of the directory.One solution is to have your container run as root and use an
ENTRYPOINT
script to make the appropriate permission changes, and then yourCMD
as an unprivileged user. For example, put the following inentrypoint.sh
:This assumes you have the
runuser
command available. You can accomplish pretty much the same thing usingsudo
instead.Use the above script by including an
ENTRYPOINT
directive in your Dockerfile:This will start the container with:
The entrypoint script will make the required permissions changes, then run
/usr/bin/myapp
asappuser
.There will throw error if host env don't have
appuser
orappgroup
, so better to use a User ID instead of user name:inside your container, run
This will show:
From host env, run:
inside your container, check
to see the user and group name, if it's not
root
, then it's should worked.