Consider the following trivial Docker file:
FROM debian:testing RUN adduser --disabled-password --gecos '' docker RUN adduser --disabled-password --gecos '' bob
in the working directory with nothing else. Create a docker image:
docker build -t test .
and then run the bash script in the container by associating the working directory with the new subdirectory in the bob home directory:
docker run --rm -it -v $(pwd):/home/bob/subdir test
Who owns the contents of subdir on the container? On the container run:
cd /home/bob/subdir ls -l
ad we see:
-rw-rw-r-- 1 docker docker 120 Oct 22 03:47 Dockerfile
Saints smoke! docker owns the content! Returning to the main machine outside the container, we see that our original user still owns the Dockerfile . Try to fix the ownership of the bob home directory. On the container run:
chown -R bob:bob /home/bob ls -l
and we see:
-rw-rw-r-- 1 bob bob 120 Oct 22 03:47 Dockerfile
But wait! outside the container, now we run ls -l
-rw-rw-r-- 1 1001 1001 120 Oct 21 20:47 Dockerfile
we no longer have our own file. Awful news!
If only one user were added in the above example, everything would be smoother. For some reason, Docker seems to create any home directory that belongs to the first non-root user that it encounters (even if that user is declared in an earlier image). Similarly, this first user is one that matches the same access rights as my home user.
Question 1 Is this correct? Can someone point me to the documentation, Iβm just guessing based on the above experiment.
Question 2 . Perhaps this is due only to the fact that both of them have the same numerical value in the kernel, and if I tested the system where my home user was not id 1000 , then permissions could be changed in each case?
Question 3 . The real question, of course, is "what should I do with this?" If bob registered as bob on this host computer, it should be able to run the container as bob and not modify the files in its host account. Be that as it may, he really needs to run the container as a docker user to avoid changing his account.
I heard you ask why I have such a weird Dockerfile? Sometimes I wonder. I am writing a container for webapp (RStudio-server) that allows other users to register, which simply uses the usernames and credentials from the Linux machine as valid usernames. This brings me, perhaps, an unusual motivation to want to create multiple users. I can get around this by creating the user only at runtime, and everything is fine. However, I am using a base image that has added a single docker user so that it can be used interactively but not run as root (according to best practice). This destroys everything, because this user becomes the first user and ends up owning everything, so login attempts as other users fail (the application cannot start due to lack of write permissions). When you run the script run chown , this problem is first solved, but at the cost of related volumes that change permissions (obviously, only a problem if we bind volumes).