Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 866
Samuël Visser Avatar asked Sep 08 '20 09:09

Samuël Visser


People also ask

Does worpress have the correct file permissions for Docker?

Using the exact same docker setup on Windows though gets me these messages within Wordpress. Clearly, Worpress doesn't have the correct file permissions. 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.

Is it possible to use Docker Compose 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. Clearly, Worpress doesn't have the correct file permissions. 1. Checking Docker for Windows settings and upgrading to WSL 2

How to allow read/write on specific drive in Docker?

In the Windows client, you can configure the docker to allow read/write on specific drives. In your case, it is the F: drive. This should fix your permissions problem. The web-server running in Docker is checking the folder permissions, which is nothing to do with windows; you need to verify the folder permissions inside Docker itself

Do I need administrator privileges to run Docker desktop?

While Docker Desktop can be run without having Administrator privileges, it does require them during installation. On installation the user gets an UAC prompt which allows a privileged helper service to be installed.


Video Answer


1 Answers

I think your issue with user being used is different, php:7.4-apache uses www-data as user. to confirm the user when you run ( replace php:7.4-apache with your image name)

docker run -d php:7.4-apache

and than run

docker exec -it YOUR_IMAGE_HASH ps aux

it should show you www-data under User columns of running processes. Once you identify the correct user, you can add to your docker file , like

FROM php:7.4-apache
.....
ARG user_id=1000
RUN usermod -u $user_id www-data
like image 120
user969068 Avatar answered Oct 10 '22 04:10

user969068