How to fix file permissions on wp-content with official wordpress image for docker-compose
You can modify your ./entrypoint.sh
script to run the default entrypoint script of the original image after it executes your chown command:
#!/bin/bashecho Fixing permissions...chown -R www-data:www-data /var/www/html/wp-content/docker-entrypoint.sh apache2-foreground
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 limitRUN 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 issuesCOPY entrypoint.sh /RUN dos2unix /entrypoint.sh && \ chmod +x /entrypoint.shENTRYPOINT ["/entrypoint.sh"]
This is due to the way the WordPress Docker image is created.
I created a Pull Request to make the wp-content
folder belong to www-data
when it is (or some of its subfolders) shared using a volume.
Before it is merged (it if is), you can create your own WordPress Docker image to integrate my change. Take one of the folder on https://github.com/docker-library/wordpress (choose PHP version, and then webserver parameters).
- Create a
docker/wordpress
folder in your project. - Copy the two raw files (Dockerfile and docker-entrypoint.sh) from GitHub into that folder.
- Replace these lines in docker-entrypoint.sh:
if [ "$(id -u)" = '0' ] && [ "$(stat -c '%u:%g' .)" = '0:0' ]; then chown "$user:$group" . fi
by:
if [ "$(id -u)" = '0' ]; then chown -R "$user:$group" . fi