How to share volumes between wordpress and wp-cli in docker-compose

Viewed 2137

In trying to setup a reusable, semi-automated docker-compose.yml file for WordPress theme development, I found a very helpful post on StackOverflow and implemented the docker-compose.yml that vstm provided.

The example works very well, and I had been using it to add wp-cli commands for adding plugins, changing options, and deleting pre-loaded themes and plugins.

But now, I'm getting nothing but permission errors and I have tried manually changing wp-cli to user: '33:33' as well as the original user:xfs

I started over from the example and the first problem I run into is adding an additional volume shared between the two for the theme I'm developing. My theme name is default so that volume name is correct.

Here's what I have so far:

version: "2.3"
services:
  db:
    image: mysql:5.7
    volumes:
      - db_data:/var/lib/mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: somewordpress
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress

  wordpress:
    depends_on:
      - db
    image: wordpress:latest
    ports:
      - 8000:80
    restart: always
    environment:
      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_NAME: wordpress
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: wordpress
      WORDPRESS_TABLE_PREFIX: "wp_"
      WORDPRESS_DEBUG: 1
    # vstm: add shared volume
    volumes:
      - wp_data:/var/www/html
      - ./default/:/var/www/html/wp-content/themes/default

  wordpress-cli:
    depends_on:
      - db
      - wordpress
    image: wordpress:cli
    # vstm: This is required to run wordpress-cli with the same
    # user-id as wordpress. This way there are no permission problems
    # when running the cli
    user: xfs
    # vstm: The sleep 10 is required so that the command is run after
    # mysql is initialized. Depending on your machine this might take
    # longer or it can go faster.
    command: >
      /bin/bash -c '
      sleep 10;
      wp core install --path="/var/www/html" --url="http://localhost:8000" --title="Local Wordpress By Docker" --admin_user=admin --admin_password=secret --admin_email=foo@bar.com;
      wp plugin install classic-editor;
      '
    # vstm: add shared volume
    volumes:
      - wp_data:/var/www/html
      - ./default/:/var/www/html/wp-content/themes/default

volumes:
  db_data:
  # vstm: add shared volume
  wp_data:

Here's the output log from the wp-cli console in kitematic:

Warning: Unable to create directory wp-content/uploads/2019/01. Is its parent directory writable by the server?
Success: WordPress installed successfully.
Installing Classic Editor (1.3)
Warning: Failed to create directory '/etc/X11/fs/.wp-cli/cache/': mkdir(): Permission denied.
Downloading installation package from https://downloads.wordpress.org/plugin/classic-editor.1.3.zip...
Unpacking the package...
Warning: Could not create directory.
Error: No plugins installed.

Everything was working perfectly on at least 5 sites and then I assume there was a change in docker recently and I updated and broke all of them. Any ideas how this can be remedied in the latest Docker?

0 Answers
Related