2

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?

1 Answer 1

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 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.