I am trying to understand the work of docker well enough to come to reasonable assurance. I use it safely. One tip for this is to always use the USER statement in the Docker file. Trying to understand the effect of this, I ran into some problems.
Concrete questions:
- What mechanism allows the host kernel to handle users that exist only in the container?
- Why does run2 below show that the directory belongs to
testuser but does not allow ls in the directory? - Why does run3 below show the directory owned by
testuser ?
Version information is at the bottom of this question.
Customization
I have the following dockerfile
FROM alpine@sha256 :1354db23ff5478120c980eca1611a51c9f2b88b61f24283ee8200bf9a54f2e5c LABEL version 2.0 LABEL description "Test image for setting user" RUN adduser -D testuser1
I will build it with
docker build -t kasterma/testuser:1 .
Then run
docker run -ti -v /home/kasterma/test-user/:/test-home kasterma/testuser:1
The directory /home/kasterma/test-user/ is the directory containing the Docker file.
Run 1: delete both lines labeled ##sometimes removed in the Docker file.
[ root@datalocal01 test-user]
Shown here is user and group as 1001; which is the user and groupid kasterma in the host. In this context, testuser has uid and gid 1000.
Besides
/ $ cd test-home sh: cd: can't cd to test-home
Run 2: delete only the second line, labeled ##sometimes removed in the Docker file.
/ $ ls -lh ... drwx------ 2 testuser testuser 40 Dec 30 14:12 test-home ...
and
/ $ cd test-home /test-home $ ls ls: can't open '.': Permission denied
Now testuser and kasterma have the same uid and gid (although they are in the container and others on the host). Why can I cd but not ls ?
Run 3: do not delete a single line labeled ##sometimes removed in the Docker file.
/ $ ls -lh ... drwx------ 2 testuser testuser 40 Dec 30 14:15 test-home ...
and
/ $ cd test-home sh: cd: can't cd to test-home
Now testuser has uid and gid 1002, so it does not match with kasterma . But the listing shows this as testuser, but the cd fails.
Version Information
OS version (running on a virtual machine in VirtualBox)
[ root@datalocal01 test-user]
and for dockers
[ root@datalocal01 test-user]