Answers: <p>One thing you must do is ensure the <code>www-data</code> user exist take this example here:</p>
  <p><a href="https://github.com/docker-library/httpd/blob/master/2.4/alpine/Dockerfile" rel="nofollow noreferrer">https://github.com/docker-library/httpd/blob/master/2.4/alpine/Dockerfile</a></p>
  <p>so you need to add something akin to:</p>
  <pre><code># ensure www-data user exists
  RUN set -x \
      &amp;&amp; adduser -u 82 -D -S -G www-data www-data
  # 82 is the standard uid/gid for &quot;www-data&quot; in Alpine
  </code></pre>
  <p>or better you can temporarily debug things and add these lines to list to see what you already have in there before adding the above:</p>
  <pre><code># Update package list and install grep
  RUN apk update &amp;&amp; apk add --no-cache grep
  
  # see a list of www-data members
  RUN grep ^www-data /etc/group
  #see if www-data exists
  RUN grep ^www-data /etc/password 
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2024-08-07T03:38:28.697Z
Answers: I'm having the same problem, and the folder clearly already exists -- it's the entire /opt/ folder. I can change its ownership manually by running the docker container, but the same command in a Dockerfile generates no errors and has no effect on the folder's ownership.
Answers: Jeffrey Benjamin Brown
Answers: CC BY-SA 4.0
Answers: 2023-02-14T20:42:58.940Z
Answers: 2023-02-14T20:42:58.940Z
Answers: Eduard Florinescu
Body: <p>I am having these two dockerfiles which in both I am adding the command <code>RUN chown -R www-data:www-data /var/www/html</code> to change the ownership to www-data user. The containers seems to build successfully with no errors, however when I ssh for example into the running php container and run <code>ls -la</code> on the workingdir I can still see all files and folders are owned by root. If I do <code>ls -la</code> outside of the running container on my local env I can see them owned by my username and staff.</p>
  <pre><code>// Apache
  FROM httpd:alpine
  
  COPY ./docker/apache/httpd-vhosts.conf /usr/local/apache2/conf/extra/httpd-vhosts.conf
  
  WORKDIR /var/www/html/
  COPY ./public /var/www/html/public
  
  RUN chown -R www-data:www-data /var/www/html
  
  EXPOSE 80
  CMD [&quot;httpd&quot;, &quot;-D&quot;, &quot;FOREGROUND&quot;]
  
  
  -----
  
  // PHP
  FROM php:8.1-fpm-alpine
  
  # Install any additional dependencies like pdo, xdebug etc
  RUN apk add --no-cache --update linux-headers \
      ${PHPIZE_DEPS} \
      openssl-dev \
      &amp;&amp; docker-php-ext-install pdo pdo_mysql
  
  # Set up the php configuration in the container
  COPY ./docker/php/php.ini /usr/local/etc/php/php.ini
  COPY ./docker/php/conf.d/*.ini /usr/local/etc/php/conf.d/
  
  # Copy the codebase in the working dir of the container
  WORKDIR /var/www/html
  COPY . /var/www/html
  
  RUN chown -R www-data:www-data /var/www/html
  
  EXPOSE 9000
  CMD [&quot;php-fpm&quot;, &quot;-y&quot;, &quot;/usr/local/etc/php-fpm.conf&quot;, &quot;-R&quot;]
  
  </code></pre>
  <p>What do I need to do to make sure the files &amp; folders are owned by www-data user within the containers?</p>
  
Tags: <docker><dockerfile>
Title: Can't change ownership of folders and files in Docker containers
UserDisplayName: ltdev
ThreadText: # Can't change ownership of folders and files in Docker containers
  
  (Asked by: username_0 on 2023-02-14)
  
  I am having these two dockerfiles which in both I am adding the command `RUN chown -R www-data:www-data /var/www/html` to change the ownership to www-data user. The containers seems to build successfully with no errors, however when I ssh for example into the running php container and run `ls -la` on the workingdir I can still see all files and folders are owned by root. If I do `ls -la` outside of the running container on my local env I can see them owned by my username and staff.
  
  ```
  // Apache
  FROM httpd:alpine
  
  COPY ./docker/apache/httpd-vhosts.conf /usr/local/apache2/conf/extra/httpd-vhosts.conf
  
  WORKDIR /var/www/html/
  COPY ./public /var/www/html/public
  
  RUN chown -R www-data:www-data /var/www/html
  
  EXPOSE 80
  CMD ["httpd", "-D", "FOREGROUND"]
  
  -----
  
  // PHP
  FROM php:8.1-fpm-alpine
  
  # Install any additional dependencies like pdo, xdebug etc
  RUN apk add --no-cache --update linux-headers \
      ${PHPIZE_DEPS} \
      openssl-dev \
      && docker-php-ext-install pdo pdo_mysql
  
  # Set up the php configuration in the container
  COPY ./docker/php/php.ini /usr/local/etc/php/php.ini
  COPY ./docker/php/conf.d/*.ini /usr/local/etc/php/conf.d/
  
  # Copy the codebase in the working dir of the container
  WORKDIR /var/www/html
  COPY . /var/www/html
  
  RUN chown -R www-data:www-data /var/www/html
  
  EXPOSE 9000
  CMD ["php-fpm", "-y", "/usr/local/etc/php-fpm.conf", "-R"]
  
  ```
  
  What do I need to do to make sure the files & folders are owned by www-data user within the containers?
  
  **Tags:** docker, dockerfile
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2023-02-14. Score: 0
  
  One thing you must do is ensure the `www-data` user exist take this example here:
  
  <https://github.com/docker-library/httpd/blob/master/2.4/alpine/Dockerfile>
  
  so you need to add something akin to:
  
  ```
  # ensure www-data user exists
  RUN set -x \
      && adduser -u 82 -D -S -G www-data www-data
  # 82 is the standard uid/gid for "www-data" in Alpine
  
  ```
  
  or better you can temporarily debug things and add these lines to list to see what you already have in there before adding the above:
  
  ```
  # Update package list and install grep
  RUN apk update && apk add --no-cache grep
  
  # see a list of www-data members
  RUN grep ^www-data /etc/group
  #see if www-data exists
  RUN grep ^www-data /etc/password 
  
  ```
  
  > **Comments:**
  > **username_2**: I'm having the same problem, and the folder clearly already exists -- it's the entire /opt/ folder. I can change its ownership manually by running the docker container, but the same command in a Dockerfile generates no errors and has no effect on the folder's ownership.  
  
  ---
  
  