Docker Container Files in user created Ownership Changes After Pulling from Dockerhub

64 views Asked by At

Take this simple Dockerfile, creates a user with adduser and then push it to dockerhub registry.

FROM ubuntu:22.04

RUN useradd --create-home -s /bin/bash user

USER user
WORKDIR /home/user

ENV PATH=/home/user/.foundry/bin:${PATH}

USER root
USER user

ENV PATH=/home/user/.local/bin:${PATH}

Build and Check the image works locally where it was just built.

dev-user@build-machine:~$ docker run --rm -it userAccount/infrastructure:owner-test-2 
dev-user@05e530a10e89:~$ 

Got a shell, no errors.. This is expected.

Next pull to a completely different / new machine and run a container

dev-user@machine-2:.../docker$ docker run --rm -it  userAccount/infrastructure:owner-test-2
Unable to find image 'userAccount/infrastructure:owner-test-2' locally
owner-test-2: Pulling from userAccount/infrastructure
aece8493d397: Pull complete 
035da186e66b: Pull complete 
4f4fb700ef54: Pull complete 
Digest: sha256:670e3125656b73f8b15fb57d0a96c2b4ee18617ffaf341f3ac6c44cdc46d3490
Status: Downloaded newer image for userAccount/infrastructure:owner-test-2
bash: /home/user/.bashrc: Permission denied
user@e4fe29e1661c:~$ 

The home folder for the created user ('user') defined in the Dockerfile is now owned by root? Why? This doesn't make any sense why this would change between a push and a pull. Nothing should get modified.

I have checked and compared the versions of buildx / docker / kernel of the host. I'm at a complete loss as to what to do next any advice / help / Someone see this before?

I can delete the image on the build-machine and pull it back from dockerhub. And it's fine. What is going on.

The expected result would be the folders would all be properly owned by the user created by adduser in the Dockerfile instead I see this if I start the container as the root user and run an 'ls -la'

dev-user@machine-2:.../docker$ docker run --user root --rm -it  userAccount/infrastructure:owner-test-2
root@defb4cf1f00f:/home/user# ls -la
total 20
drwxr-x--- 2 root root 4096 Nov  1 20:48 .
drwxr-xr-x 1 root root 4096 Nov  1 20:48 ..
-rw-r--r-- 1 root root  220 Jan  6  2022 .bash_logout
-rw-r--r-- 1 root root 3771 Jan  6  2022 .bashrc
-rw-r--r-- 1 root root  807 Jan  6  2022 .profile
root@defb4cf1f00f:/home/user# 
0

There are 0 answers