7

I am logged in in my PC (Fedora 24) as rperez. I have setup Docker for being able to run through this user, so I am running a container as follow:

$ docker run -d \
             -it \
             -e HOST_IP=192.168.1.66 \
             -e PHP_ERROR_REPORTING='E_ALL & ~E_STRICT' \
             -p 80:80 \
             -v ~/var/www:/var/www \
             --name php55-dev reypm/php55-dev

Notice the $ sign meaning I am running the command as a non root user (which uses #). The command above creates the following directory: /home/rperez/var/www but owner is set to root I believe this is because docker run as root user behind scenes.

Having this setup I am not able to create a file under ~/var/www as rperez because the owner is root so ...

What is the right way to deal with this? I have read this and this but is not so helpful.

Any help?

Community
  • 1
  • 1
ReynierPM
  • 17,594
  • 53
  • 193
  • 363
  • Do you mean `chown $(whoami) /path/to/your/dir`? – Kane Blueriver Sep 30 '16 at 15:19
  • @kxxoling I know that's the right command but why should I need to run such command on every directory created by Docker all the time? Should be a command or a flag or a work around to avoid this and that's what I am looking for :) – ReynierPM Sep 30 '16 at 15:20
  • You can create the volume directory before running the container, so the directory will be owned by current user. – Kane Blueriver Sep 30 '16 at 15:29
  • 1
    I found a discussion here: https://github.com/docker/docker/issues/3206 It's an expected behavior. You can get a solution there, but I think my way is easier to use. – Kane Blueriver Sep 30 '16 at 15:33
  • @kxxoling Creating the directory first did works pls answer this with your solution so I can accept your answer and thanks – ReynierPM Sep 30 '16 at 15:42

2 Answers2

8

As discussioned here, this is an expected behavior of docker. You can create the target volume directory before running docker command or change the owner to your current user after the directory is created by docker:

chown $(whoami) -R /path/to/your/dir
Kane Blueriver
  • 4,170
  • 4
  • 29
  • 48
1

I hit this same issue (also in a genomics context for the very same reason) and also found it quite unintuitive. What is the recommended way to "inherit ownership". Sorry if this described elsewhere, but I couldn't find it. Is it something like:

docker run ... -u $(id -u):$(id -g) ...
Razzwan
  • 137
  • 7