Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to create a docker image/container with same file rights as host user

Tags:

docker

When start a docker container as user with name 'username1' in group 'usergroup1'.

And that container has files/folders on the local file system with volume: eg.

$username1>docker run -v /homes/username1/output:output outputter

The files are created with root as owner.

What do i need to do in the Dockerfile or startup options to make sure the file rigths in the output folder are the same as the localuser:group, in this case username1:usergroup1?

like image 860
Jan vO Avatar asked Jan 05 '16 11:01

Jan vO


Video Answer


2 Answers

As explained in this project:

By default, our docker containers run as the root user. Files created or modified by the container will thus become owned by the root user, even after quitting the container.
To avoid this problem, it is necessary to run the container using a non-root user.

If the host machine user has a UID other than 1000 (or 0, for root), the user should specify their UID when running docker, e.g.

docker run -d -p 8787:8787 -v $(pwd):/home/$USER/foo \
  -e USER=$USER  -e USERID=$UID rocker/rstudio

to avoid changing the permissions in the linked volume on the host

Here that works because that project Dockerfile, when starting the container, creates a user with the same uid (name is not important)

## (Docker cares only about uid, not username; diff users with same uid = confusion)
if [ "$USERID" -ne 1000 ]
## Configure user with a different USERID if requested.
    then
        echo "creating new $USER with UID $USERID"
        useradd -m $USER -u $USERID
        mkdir /home/$USER
        chown -R $USER /home/$USER
like image 161
VonC Avatar answered Sep 28 '22 08:09

VonC


You are going to have to wait for user namespace support, hopefully later this year.

like image 21
Michael Avatar answered Sep 28 '22 10:09

Michael