简体   繁体   中英

Wordpress on Docker: Could not create directory on mounted volume

Here is are the original files in the Wordpress Docker container on path /var/www/html :

$ docker exec 5b957c7b9c5ad054883694afbfb80d3c9df6707458d55011f471be0701f3890c ls -l
total 192
-rw-r--r--  1 www-data www-data   418 Sep 25  2013 index.php
-rw-r--r--  1 www-data www-data 19935 Jan  2 18:51 license.txt
-rw-r--r--  1 www-data www-data  7433 Jan 11 17:46 readme.html
-rw-r--r--  1 www-data www-data  5447 Sep 27  2016 wp-activate.php
drwxr-xr-x  9 www-data www-data  4096 May 16 21:50 wp-admin
-rw-r--r--  1 www-data www-data   364 Dec 19  2015 wp-blog-header.php
-rw-r--r--  1 www-data www-data  1627 Aug 29  2016 wp-comments-post.php
-rw-r--r--  1 www-data www-data  2764 May 29 22:19 wp-config-sample.php
-rw-r--r--  1 www-data www-data  3148 May 29 22:19 wp-config.php
drwxr-xr-x  4 www-data www-data  4096 May 16 21:50 wp-content
-rw-r--r--  1 www-data www-data  3286 May 24  2015 wp-cron.php
drwxr-xr-x 18 www-data www-data 12288 May 16 21:50 wp-includes
-rw-r--r--  1 www-data www-data  2422 Nov 21  2016 wp-links-opml.php
-rw-r--r--  1 www-data www-data  3301 Oct 25  2016 wp-load.php
-rw-r--r--  1 www-data www-data 33939 Nov 21  2016 wp-login.php
-rw-r--r--  1 www-data www-data  8048 Jan 11 05:15 wp-mail.php
-rw-r--r--  1 www-data www-data 16255 Apr  6 18:23 wp-settings.php
-rw-r--r--  1 www-data www-data 29896 Oct 19  2016 wp-signup.php
-rw-r--r--  1 www-data www-data  4513 Oct 14  2016 wp-trackback.php
-rw-r--r--  1 www-data www-data  3065 Aug 31  2016 xmlrpc.php

I am trying to start the Wordpress container with a mounted Docker volume to be able to store the custom files persistently:

$ sudo docker run -p 80:80 --link some-mysql:mysql  -v /var/www:/var/www/html --name docker-wordpress -d wordpress

The problem is that even when exactly replicating the ownership and priviledges on local files in /var/www :

$ sudo chown -R www-data:www-data /var/www
$ sudo find /var/www/ -type d -exec chmod 755 {} \;
$ sudo find /var/www/ -type f -exec chmod 644 {} \;

I am still getting an error of this kind, when running Wordpress inside the container:

Could not create directory

How to set the privileges properly to make sure Wordpress is able to write into the mounted Docker volume?

Looking at your error message i come to the conclusion that you are trying to install a plugin or update wordpress itself

This issue is slightly tricky to figure out.

executing chown -R www-data:www-data /var/www to set correct user:group permissions should technically solve it, however ..

On a new wordpress install the upload & plugins folder does not yet exist and therefore when installer trying to create a plugins/subfolder it will throw the error.

How to Fix Wordpress / Docker Plugin install Permission issue

Fixing this issue is however quite easy once grasping it.

Option A

in your .Docker file add following towards the very end but before any [CMD] command.

RUN mkdir /var/www/html/wp-content/plugins
RUN mkdir /var/www/html/wp-content/uploads
RUN chown -R www-data:www-data /var/www
RUN find /var/www/ -type d -exec chmod 0755 {} \\;
RUN find /var/www/ -type f -exec chmod 644 {} \\;

Option B

ssh in to your docker container
docker exec -it <container_name> /bin/bash

If you don't know the container name find it with
docker ps

Simply run same commands as in example above
$ mkdir /var/www/html/wp-content/plugins
$ mkdir /var/www/html/wp-content/uploads
$ chown -R www-data:www-data /var/www
$ find /var/www/ -type d -exec chmod 0755 {} \\;
$ find /var/www/ -type f -exec chmod 644 {} \\;

Building up on Mathias Asberg answer, you can simply create 2 folder ie plugins and uploads update the permission to 777 and mount the volumes

Steps:

  • mkdir plugins uploads
  • chmod -R 777 plugins uploads
  • update docker-compose.yaml to mount volume
  volumes: # 
  - ./plugins:/var/www/html/wp-content/plugins
  - ./uploads:/var/www/html/wp-content/uploads

I had a similar issue where I was using a mounted volume and wanted to ensure files could be managed on both the host operating system and by the WordPress docker image.

Within docker, the WordPress site runs as a user www-data with UID 82; this also has an associated www-data group, again with a group id of 82. By creating a group on the host OS with the same id as www-data and changing the group ownership to that group both host and docker image can manage the files.

In this example we add give access rights to a user named ec2-user on the host OS; all scripts are run on host OS:

  • First check there is an existing user or group on the host with the same id as www-data:
getent group | grep 82
cat /etc/passwd | grep 82
  • Assuming there is no user/group conflict, create a group on host with same group id as the www-data group within docker and add any required users to that group; this example uses the same group name on the host:
sudo groupadd -g 82 www-data
sudo usermod -a -G www-data ec2-user
  • Finally, change the group ownership of files and update permissions to give the www-data group read and write permissions on both host and in docker:
sudo chgrp -R www-data ~/repos/site/html
find ~/repos/site/html -type d -exec chmod 0775 {} \;
find ~/repos/site/html -type f -exec chmod 664 {} \;

This results in the follow permissions within the docker image, where both the ec2-user on the host and www-data with docker can read/write all files and folders within html:

bash-5.0# ls -lah
total 248K
drwxrwxr-x    5 1000     www-data    4.0K Apr 25 09:03 .
drwxr-xr-x    3 root     root          18 Oct 21  2019 ..
-rw-rw-r--    1 1000     www-data     405 Apr 23 12:21 index.php
-rw-rw-r--    1 1000     www-data   19.1K Apr 23 12:21 license.txt
-rw-rw-r--    1 1000     www-data    7.3K Apr 23 12:21 readme.html
-rw-rw-r--    1 1000     www-data    7.0K Apr 23 12:21 wp-activate.php
drwxrwxr-x    9 1000     www-data    4.0K Apr 23 12:22 wp-admin
-rw-rw-r--    1 1000     www-data     351 Apr 23 12:21 wp-blog-header.php
-rw-rw-r--    1 1000     www-data    2.3K Apr 23 12:21 wp-comments-post.php
-rw-rw-r--    1 1000     www-data    2.8K Apr 23 12:35 wp-config-sample.php
-rw-rw-r--    1 1000     www-data    3.4K Apr 23 12:35 wp-config.php
drwxrwxr-x    6 1000     www-data      84 Apr 23 12:22 wp-content
-rw-rw-r--    1 1000     www-data    3.8K Apr 23 12:22 wp-cron.php
drwxrwxr-x   26 1000     www-data   12.0K Apr 23 12:22 wp-includes
-rw-rw-r--    1 1000     www-data    2.4K Apr 23 12:22 wp-links-opml.php
-rw-rw-r--    1 1000     www-data    3.8K Apr 23 12:22 wp-load.php
-rw-rw-r--    1 1000     www-data   46.8K Apr 23 12:22 wp-login.php
-rw-rw-r--    1 1000     www-data    8.4K Apr 23 12:22 wp-mail.php
-rw-rw-r--    1 1000     www-data   22.5K Apr 23 12:22 wp-settings.php
-rw-rw-r--    1 1000     www-data   31.2K Apr 23 12:22 wp-signup.php
-rw-rw-r--    1 1000     www-data    4.6K Apr 23 12:22 wp-trackback.php
-rw-rw-r--    1 1000     www-data    3.2K Apr 23 12:22 xmlrpc.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM