Skip to content
Advertisement

Permission problem: How to setup permissions on docker for windows for use with WordPress

The problem

I’m trying to setup my developing environment using Docker for Windows for use with WordPress. I’m using docker compose with a custom Dockerfile. This works perfectly on MacOS. Using the exact same docker setup on Windows though gets me these messages within WordPress.

Trying to upload media Trying to upload media

Trying to update WordPress Trying to update WordPress

Clearly, Worpress doesn’t have the correct file permissions.

What I tried

1. Checking Docker for Windows settings and upgrading to WSL 2

I’m using the WSL 2 based engine now, which should give full root permissions to all the files on the system. I upgraded to WPL 2 as I was first using the Hyper-V- based backend (with of course the correct file permissions setup), I tried to fix the problems by upgrading. No luck.

2. Experimenting with chmod and chown

First, I added chmod -R 777 /var/www/html/ to the Dockerfile. As far as I know, this should give all file permissions to root. It didn’t have any effect. So maybe I’m using a different user? The command whoami did give me root back though.
Maybe I did something wrong and the user is something else. So I added chown -R www-data:www-data /var, as I saw www-data should be the default Docker user and group. No luck.

Just for the fun of it, I also tried chmod -R 777 /var/www/html/wp-content/uploads/ just to be more specific in the path. Interestingly, this gave me the error chmod: cannot access '/var/www/html/wp-content/uploads/': No such file or directory. I did link the folders though and this works (I can see in the folder structure in IntelliJ the files indeed are in /var/www/html). The -R option should make this recursive anyway, so it shouldn’t matter.

3. Doing all this while the container is running

So maybe because the files were not yet present, I could not assign permissions. So I tried all this also when the container was actually running. Again, no luck.

4. Running as user root

First, I added user: root to the service in my docker-compose.yml. No Luck.
Then I added USER root to the Dockerfile, just below FROM php:7.4-apache. No luck.

5. Using the official WordPress image

As you can see below, I’m using the apache image as a basis for my Dockerfile. I also tried using the wordpress:latest image directly from my docker-compose.yml (omitting the entire Dockerfile) and I tried using FROM: wordpress:latest on top of the Dockerfile. Both didn’t change anything.

My files

By now, I tried every solution I could find on the internet and nothing works. Crazy thing, all this works fine under MacOS. Here are my docker files, I hope u guys can help me out here.

docker-compose.yml

services:
  web:
    build:
      context: ./
      dockerfile: .docker/Dockerfile
    container_name: wolfpackvision.com
    ports:
    - "8080:80"
    volumes:
    - .:/var/www/html

Dockerfile

FROM php:7.4-apache

#USER root

RUN apt-get update
RUN docker-php-ext-install mysqli

## Install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

## Install PHP-GD
RUN apt-get install -y libpng-dev libjpeg-dev libfreetype6-dev 
    && docker-php-ext-configure gd --with-freetype=/usr/include/ --with-jpeg=/usr/include/ 
    && docker-php-ext-install gd

## Install xdebug
RUN apt-get install --assume-yes --fix-missing git libzip-dev libmcrypt-dev openssh-client 
    libxml2-dev libpng-dev g++ make autoconf 
    && docker-php-source extract 
    && pecl install xdebug redis 
    && docker-php-ext-enable xdebug redis 
    && docker-php-source delete 
    && docker-php-ext-install pdo_mysql soap intl zip

## Configure xdebug
RUN echo "xdebug.remote_enable=on" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini 
    && echo "xdebug.remote_autostart=off" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini 
    && echo "xdebug.remote_port=9000" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini 
    && echo "xdebug.remote_handler=dbgp" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini 
    && echo "xdebug.remote_connect_back=0" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini 
    && echo "xdebug.idekey=wolfpackvision.com" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini 
    && echo "xdebug.remote_host=host.docker.internal" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini

## Enable mod_rewrite http://httpd.apache.org/docs/current/mod/mod_rewrite.html & mod_headers http://httpd.apache.org/docs/current/mod/mod_headers.html
RUN a2enmod rewrite 
    && a2enmod headers

## Give Full folder permissions to server
#RUN chown -R www-data:www-data /var/www/html
#RUN chmod -R 777 /var/www/html/
#RUN chmod -R 777 /var/www/html/wp-content/uploads/
#RUN chmod -R 777 /var/www/html/
#RUN chmod -R 766 /var/www/html/

## Copy php.ini over
COPY ./.docker/php/php.ini /usr/local/etc/php

## Cleanup
RUN rm -rf /tmp/*

-> Please don’t go warn me about 777, I know about that. This is all strictly local and I will never use this in production. Plus, if I get the permissions working, I might tighten it. First I want it to work at all.

Edit

In response to @user969068.
`docker exec -it ps aux` gives me:
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.1  0.0  90652 28568 ?        Ss   11:04   0:00 apache2 -DFOREG
www-data    16  0.0  0.0  90684  8176 ?        S    11:04   0:00 apache2 -DFOREG
www-data    17  0.0  0.0  90684  8176 ?        S    11:04   0:00 apache2 -DFOREG
www-data    18  0.0  0.0  90684  8176 ?        S    11:04   0:00 apache2 -DFOREG
www-data    19  0.0  0.0  90684  8176 ?        S    11:04   0:00 apache2 -DFOREG
www-data    20  0.0  0.0  90684  8176 ?        S    11:04   0:00 apache2 -DFOREG
root        21  0.0  0.0   7640  2708 pts/0    Rs+  11:06   0:00 ps aux

I already tried to do what you recommended with PID 21, 1 and 16. All three had the same result; no file permissions. What am I missing here?

Advertisement

Answer

Thanks for your help. Turns out, the issue had nothing to do with Docker. WordPress was configured to find the uploads directory within my host folder structure, setting it to /wp-content/uploads fixed everything.

Thanks for your help anyway!

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement