Problem


I'm setting up a Wordpress local environment with docker-compose and the official wordpress image from the docker repository.

I am on windows.

The problem is that I have some premissions issues on the wp-content, and I am not able to upload files from my Wordpress admin panel. wordpress error unable to create directory

What I've already done


I checked then the file permissions inside the container, and this was the output:

file permissions in the wordpress container

As you can see, the owner of my wp-content is root instead of www-data.

The immediate solution is to open the container's bash and give
chown -R www-data:www-data /var/www/html/wp-content/

This of course works, but I don't want to do this process every time I start a new wordpress project. To achieve this I've created a Dockerfile like this:

FROM wordpress:5.1.1

# install dos2unix (fix problem between CRLF and LF)
RUN apt-get update
RUN apt-get install -y dos2unix

# increase upload limit
RUN touch /usr/local/etc/php/conf.d/uploads.ini \
    && echo "upload_max_filesize = 10M;" >> /usr/local/etc/php/conf.d/uploads.ini

# fix permissions issues
COPY entrypoint.sh /
RUN dos2unix /entrypoint.sh
RUN chmod +x /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]

And my entrypoint.sh looks like this:

#!/bin/bash
echo Fixing permissions...
chown -R www-data:www-data /var/www/html/wp-content/

But then I realized that I was overriding the ENTRYPOINT of the original wordpress image, and the container exited always with code 0.

Then I tried with CMD instead of ENTRYPOINT, and I changed my Dockerfile like this:

FROM wordpress:5.1.1

# increase upload limit
RUN touch /usr/local/etc/php/conf.d/uploads.ini \
    && echo "upload_max_filesize = 10M;" >> /usr/local/etc/php/conf.d/uploads.ini

CMD chown -R www-data:www-data /var/www/html/wpcontent/

But I receive always the error that the file or folder doesn't exists.

I've tried to use CMD also like this:
CMD ["chown", "-R", "www-data:www-data", "/var/www/html/wp-content/"]
but without success.

Question


There is a way to run a command after the original ENTRYPOINT?
Do you know otherwise a better way to solve this problem?

1 Answers

0
Community On

From Docker's documentation the command you should be using is RUN.

Don’t confuse RUN with CMD. RUN actually runs a command and commits the result; CMD does not execute anything at build time, but specifies the intended command for the image.

So the line in your Dockerfile should be: RUN chown -R www-data:www-data /var/www/html/wp-content

Also, to decrease the number of layers created and the size of your image, I'd chain as many of the RUN commands as possible. For example (not tested):

FROM wordpress:5.1.1

# install dos2unix (fix problem between CRLF and LF) and increase upload limit
RUN apt-get update -y && \
    apt-get install -y dos2unix && \
    touch /usr/local/etc/php/conf.d/uploads.ini \
    && echo "upload_max_filesize = 10M;" >> /usr/local/etc/php/conf.d/uploads.ini && \
    chown -R www-data:www-data /var/www/html/wp-content

# fix permissions issues
COPY entrypoint.sh /
RUN dos2unix /entrypoint.sh && \
    chmod +x /entrypoint.sh

ENTRYPOINT ["/entrypoint.sh"]