I have a local folder (code) on my system, which belongs to my user.The local folder path is something like this:
- home-- my_user---- work------ projects-------- code
I want to run a Docker container and mount this folder in the container. I want this mounted folder to belong to the user I have on the local host, so I can access files, create them inside the container, and still own them on the local host.
Docker is running in rootless mode.
I'm using this Dockerfile:
FROM ubuntuARG UIDARG GIDARG USERNAME# Install necessary packages including python3-virtualenv and pipRUN apt-get update && apt-get install -y \ sudo \ wget \ python3 \ python3-pip \ python3-virtualenv \ python3-dev \ build-essential \ gitRUN ln -sf /usr/bin/python3 /usr/bin/python && \ ln -sf /usr/bin/pip3 /usr/bin/pip# Create a group with the specified GIDRUN groupadd -g ${GID} ${USERNAME}# Create a user with the specified UID and GID (-d creates home folder, -s selects shell)RUN useradd -d /home/${USERNAME} -u ${UID} -g ${GID} -s /bin/bash ${USERNAME}# Add the user to the sudoers fileRUN echo "${USERNAME} ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoersRUN mkdir /home/${USERNAME}/code && chown ${USERNAME} /home/${USERNAME}/codeRUN echo "${USERNAME}"USER ${USERNAME}
I'm building the image with:
docker build --build-arg UID=$(id -u) --build-arg GID=$(id -g) --build-arg USERNAME=$(whoami) -t image_name -f Dockerfile .
And running it with:
docker run -it -v /home/my_user/work/projects/code/:/home/my_user/code image_name
Inside the container, my username, id and gid are the same as on the local host.
However, if I check the permissions, I see that the code folder still belongs to root:
total 8drwxrwxr-x 10 root root 4096 Jul 11 14:41 code
I tried running chown again from the container:
chown -R my_user code/
but I get:
chown: changing ownership of 'code/xxx': Operation not permitted
for all the files in the code folder.