How to use mount volumes with rw permissions when using Ubuntu 22.04 container with a non-root user

138 views Asked by At

I trying to create a docker instance using this dockerfile.

FROM ubuntu:22.04

RUN apt-get update -y

RUN adduser --disabled-password --gecos "" username && chown -R username:username /home/username

I build the docker image with docker build . --tag sample/ubuntu:22.04. When I run docker instance using docker run --rm -it --userns=host --user=username sample/ubuntu:22.04 /bin/bash, I got permission denied errors when using the home directory.

bash: /home/username/.bashrc: Permission denied
username@2a9dde049ccb:/$ cd /home/username/
bash: cd: /home/username/: Permission denied
username@2a9dde049ccb:/$ ls /home/username/
ls: cannot open directory '/home/username/': Permission denied

If I use "root" user instead of "username", I can list the contents of /home/username directory without any permission error.

If I remove "userns" argument, I can enter the /home/username but I can't use mounted volumes.

docker run --rm -it -v /home/utkusavas/tmp_dir:/home/username/project --user=username sample/ubuntu:22.04 /bin/bash

username@8827f7d085bf:~$ ls -al
total 32
drwxr-x--- 1 username   username     4096 Dec 12 04:48 .
drwxr-xr-x 1 root       root         4096 Dec 12 04:28 ..
-rw-r--r-- 1 username   username     220  Dec 12 04:28 .bash_logout
-rw-r--r-- 1 username   username     3771 Dec 12 04:28 .bashrc
drwxrwxr-x 6 nobody     nogroup      4096 Dec  8 08:18 .config
-rw-r--r-- 1 username   username     807  Dec 12 04:28 .profile
drwxrwxr-x 7 nobody     nogroup      4096 Dec  4 12:09 project

If I use Ubuntu18.04 instead Ubuntu22.04, I can use the home directory with username without any permission denied error.

FROM ubuntu:18.04

RUN apt-get update -y

RUN adduser --disabled-password --gecos "" username && chown -R username:username /home/username
username@0d77e104458d:/home$ ls -al /home/username/
total 20
drwxr-xr-x 2 166536 166536 4096 Dec 12 04:54 .
drwxr-xr-x 1 165536 165536 4096 Dec 12 04:54 ..
-rw-r--r-- 1 166536 166536  220 Dec 12 04:54 .bash_logout
-rw-r--r-- 1 166536 166536 3771 Dec 12 04:54 .bashrc
-rw-r--r-- 1 166536 166536  807 Dec 12 04:54 .profile

Operating system : Ubuntu 20.04.6

Docker version : Docker version 24.0.7, build afdd53b

/etc/docker/daemon.json

{
    "userns-remap": "default"
}

/etc/subuid

...
dockremap:165536:6553

/etc/subgid

...
dockremap:165536:65536

Why am I getting permission denied error when I use Ubuntu 22.04 as a base image but not with Ubuntu 18.04? I did the same experiments with Ubuntu 20.04 and Ubuntu 23.04. Ubuntu 20.04 worked fine but I got same errors on Ubuntu 23.04.

0

There are 0 answers