Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WP-CLI file permission problems using Docker volumes

wordpress:cli container is not able to manipulate files on a volume shared with wordpress container.

Here's the docker-compose.yml file I use to bootstrap WordPress:

version: "3"

services:
  wordpress:
    image: wordpress
    ports: ["80:80"]
    volumes: ["wp_test:/var/www/html"]
    environment:
      WORDPRESS_DB_NAME: wordpress
      WORDPRESS_DB_USER: root
      WORDPRESS_DB_PASSWORD: secret

  mysql:
    image: mysql
    environment:
      MYSQL_ROOT_PASSWORD: secret

  cli:
    image: wordpress:cli
    volumes: ["wp_test:/var/www/html"]
    command: sh -c "sleep 20 && wp core install --path=/var/www/html --url=localhost --title=test --admin_user=test --admin_password=test [email protected]"

volumes:
  wp_test:

I start it with docker-compose up and when wp-cli sets up the installation, there is a warning:

cli_1        | Warning: Unable to create directory wp-content/uploads/2018/02. Is its parent directory writable by the server?

The installation works so far, but I'd like to fix this warning, because for other wp-cli tasks manipulating files is critical.

From inside wordpress container, privileges look like this:

drwxr-xr-x  5 www-data www-data  4096 Feb  2 11:21 .
drwxr-xr-x  4 root     root      4096 Jan  4 01:30 ..
-rw-r--r--  1 www-data www-data   234 Feb  2 11:21 .htaccess
-rw-r--r--  1 www-data www-data   418 Sep 25  2013 index.php
-rw-r--r--  1 www-data www-data 19935 Jan  6 19:32 license.txt
-rw-r--r--  1 www-data www-data  7413 Dec 12  2016 readme.html
-rw-r--r--  1 www-data www-data  5434 Sep 23 12:21 wp-activate.php
drwxr-xr-x  9 www-data www-data  4096 Jan 16 21:39 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 Feb  2 11:21 wp-config-sample.php
-rw-r--r--  1 www-data www-data  3144 Feb  2 11:21 wp-config.php
drwxr-xr-x  4 www-data www-data  4096 Feb  2 11:22 wp-content
-rw-r--r--  1 www-data www-data  3669 Aug 20 04:37 wp-cron.php
drwxr-xr-x 18 www-data www-data 12288 Jan 16 21:39 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  3306 Aug 22 11:52 wp-load.php
-rw-r--r--  1 www-data www-data 36583 Oct 13 02:10 wp-login.php
-rw-r--r--  1 www-data www-data  8048 Jan 11  2017 wp-mail.php
-rw-r--r--  1 www-data www-data 16246 Oct  4 00:20 wp-settings.php
-rw-r--r--  1 www-data www-data 30071 Oct 18 17:36 wp-signup.php
-rw-r--r--  1 www-data www-data  4620 Oct 23 22:12 wp-trackback.php
-rw-r--r--  1 www-data www-data  3065 Aug 31  2016 xmlrpc.php

but from within cli container the same volume looks like this:

drwxr-xr-x    5 www-data www-data      4096 Feb  2 11:21 .
drwxr-xr-x    3 root     root          4096 Jan 10 07:36 ..
-rw-r--r--    1 xfs      xfs            234 Feb  2 11:21 .htaccess
-rw-r--r--    1 xfs      xfs            418 Sep 25  2013 index.php
-rw-r--r--    1 xfs      xfs          19935 Jan  6 19:32 license.txt
-rw-r--r--    1 xfs      xfs           7413 Dec 12  2016 readme.html
-rw-r--r--    1 xfs      xfs           5434 Sep 23 12:21 wp-activate.php
drwxr-xr-x    9 xfs      xfs           4096 Jan 16 21:39 wp-admin
-rw-r--r--    1 xfs      xfs            364 Dec 19  2015 wp-blog-header.php
-rw-r--r--    1 xfs      xfs           1627 Aug 29  2016 wp-comments-post.php
-rw-r--r--    1 xfs      xfs           2764 Feb  2 11:21 wp-config-sample.php
-rw-r--r--    1 xfs      xfs           3144 Feb  2 11:21 wp-config.php
drwxr-xr-x    4 xfs      xfs           4096 Feb  2 11:22 wp-content
-rw-r--r--    1 xfs      xfs           3669 Aug 20 04:37 wp-cron.php
drwxr-xr-x   18 xfs      xfs          12288 Jan 16 21:39 wp-includes
-rw-r--r--    1 xfs      xfs           2422 Nov 21  2016 wp-links-opml.php
-rw-r--r--    1 xfs      xfs           3306 Aug 22 11:52 wp-load.php
-rw-r--r--    1 xfs      xfs          36583 Oct 13 02:10 wp-login.php
-rw-r--r--    1 xfs      xfs           8048 Jan 11  2017 wp-mail.php
-rw-r--r--    1 xfs      xfs          16246 Oct  4 00:20 wp-settings.php
-rw-r--r--    1 xfs      xfs          30071 Oct 18 17:36 wp-signup.php
-rw-r--r--    1 xfs      xfs           4620 Oct 23 22:12 wp-trackback.php
-rw-r--r--    1 xfs      xfs           3065 Aug 31  2016 xmlrpc.php

While user in cli container is www-data (just like it is in wordpress container), it cannot create the desired folder:

$ mkdir -p wp-content/uploads/2018/02
mkdir: can't create directory 'wp-content/uploads/': Permission denied

I'm running docker compose 1.18.0 and and docker engine 17.12.0-ce on a mac.

I also created a github issue.

like image 751
Mikhail Vasin Avatar asked Feb 02 '18 11:02

Mikhail Vasin


1 Answers

I assume your question is "How do I fix WP-CLI file permission problems using Docker volumes?"

You need to make sure to run wp-cli as the same UID in both containers.

From the command line:

docker run -it --rm \
    --volumes-from $container \
    --network container:$container \
    --user 33:33 \
    wordpress:cli core install --path=/var/www/html --url=localhost --title=test --admin_user=test --admin_password=test [email protected]

--user 33:33 is a workaround to run wp-cli with the same UID/GID as www-data in the WordPress container. Problems arise because the containers have different UID/GID for www-data. See https://github.com/docker-library/wordpress/issues/256

You should be able to add user: "33:33" to your docker-compose file as well. See https://github.com/docker/compose/issues/1532

like image 162
Brian Wong Avatar answered Dec 03 '22 07:12

Brian Wong