How to fix docker wpcli Warning: Failed to create directory "/var/www/html/wp-content/upgrade/..."

Problem:

When trying to update a plugin or similar action using the wordpress:cli wpcli docker image, for example using a command such as

docker-compose exec wpcli wp plugin update google-sitemap-generator

you see an error message such as

Warning: Failed to create directory. "/var/www/html/wp-content/upgrade/google-sitemap-generator.4.1.16"
+--------------------------+-------------+-------------+--------+
| name                     | old_version | new_version | status |
+--------------------------+-------------+-------------+--------+
| google-sitemap-generator | 4.1.13      | 4.1.16      | Error  |
+--------------------------+-------------+-------------+--------+
Error: No plugins updated (1 failed).

Solution

This error occurs because the wordpress image (without :cli!) is based on Debian and the wordpress:cli image is based on Alpine Linux. Debian uses the UID 33 for the www-data user whereas Alpine Linux uses 83. So to fix the permission problem, you need to force the cli image to use 33:

This is documented on the wordpress docker page.

docker-compose exec -e HOME=/tmp --user 33:33 wpcli wp plugin update google-sitemap-generator