Body: <p>I have a local folder (code) on my system, which belongs to my user.
  The local folder path is something like this:</p>
  <pre><code>- home
  -- my_user
  ---- work
  ------ projects
  -------- code
  </code></pre>
  <p>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.<br />
  <em><strong>Docker is running in rootless mode.</strong></em></p>
  <p>I'm using this Dockerfile:</p>
  <pre><code>FROM ubuntu
  
  ARG UID
  ARG GID
  ARG USERNAME
  
  # Install necessary packages including python3-virtualenv and pip
  RUN apt-get update &amp;&amp; apt-get install -y \
      sudo \
      wget \
      python3 \
      python3-pip \
      python3-virtualenv \
      python3-dev \
      build-essential \
      git
  RUN ln -sf /usr/bin/python3 /usr/bin/python &amp;&amp; \
      ln -sf /usr/bin/pip3 /usr/bin/pip
  
  # Create a group with the specified GID
  RUN 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 file
  RUN echo &quot;${USERNAME} ALL=(ALL) NOPASSWD:ALL&quot; &gt;&gt; /etc/sudoers
  RUN mkdir /home/${USERNAME}/code &amp;&amp; chown ${USERNAME} /home/${USERNAME}/code
  RUN echo &quot;${USERNAME}&quot;
  USER ${USERNAME}
  </code></pre>
  <p>I'm building the image with:</p>
  <pre><code>docker build --build-arg UID=$(id -u) --build-arg GID=$(id -g) --build-arg USERNAME=$(whoami) -t image_name -f Dockerfile .
  </code></pre>
  <p>And running it with:</p>
  <pre><code>docker run -it -v /home/my_user/work/projects/code/:/home/my_user/code image_name
  </code></pre>
  <p>Inside the container, my username, id and gid are the same as on the local host.<br />
  However, if I check the permissions, I see that the code folder still belongs to root:</p>
  <pre><code>total 8
  drwxrwxr-x 10 root root 4096 Jul 11 14:41 code
  </code></pre>
  <p>I tried running chown again from the container:</p>
  <pre><code>chown -R my_user code/
  </code></pre>
  <p>but I get:</p>
  <pre><code>chown: changing ownership of 'code/xxx': Operation not permitted
  </code></pre>
  <p>for all the files in the code folder.</p>
  
Comments: CC BY-SA 4.0
Comments: 2024-07-16T11:27:35.550Z
Comments: I guess this happens because the folder is getting replaced by the folder in your system. Since the username or group of the folder in your system doesn't exist in the system, it is choosing the group ID and User ID of the folder in the system. So you might need to use an `entrypoint.sh` file to change the ownership while running the container.
Comments: Ajay
Comments: CC BY-SA 4.0
Comments: 2024-07-16T11:28:26.627Z
Comments: Also, use `-R` flag in the `chown` command to reflect the subfolders and files inside the folder.
Comments: Ajay
Tags: <docker><dockerfile>
Title: Run Docker with same user as on local host and own mounted folders
UserDisplayName: Carlo
ThreadText: # Run Docker with same user as on local host and own mounted folders
  
  (Asked by: username_0 on 2024-07-15)
  
  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 ubuntu
  
  ARG UID
  ARG GID
  ARG USERNAME
  
  # Install necessary packages including python3-virtualenv and pip
  RUN apt-get update && apt-get install -y \
      sudo \
      wget \
      python3 \
      python3-pip \
      python3-virtualenv \
      python3-dev \
      build-essential \
      git
  RUN ln -sf /usr/bin/python3 /usr/bin/python && \
      ln -sf /usr/bin/pip3 /usr/bin/pip
  
  # Create a group with the specified GID
  RUN 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 file
  RUN echo "${USERNAME} ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers
  RUN mkdir /home/${USERNAME}/code && chown ${USERNAME} /home/${USERNAME}/code
  RUN 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 8
  drwxrwxr-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.
  
  **Tags:** docker, dockerfile
  
  > **Comments:**
  > **username_1**: I guess this happens because the folder is getting replaced by the folder in your system. Since the username or group of the folder in your system doesn't exist in the system, it is choosing the group ID and User ID of the folder in the system. So you might need to use an `entrypoint.sh` file to change the ownership while running the container.  
  > **username_1**: Also, use `-R` flag in the `chown` command to reflect the subfolders and files inside the folder.  
  
  ---
  
  ## 0 answers
  
  