How to run wp cli in docker-compose.yml

后端 未结 2 1466
深忆病人
深忆病人 2021-01-31 11:53

Just starting in docker here

So I got this in my docker-compose.yml

version: \'3.3\'

services:
  db:
    image: mysql:5.7
    volumes:
         


        
相关标签:
2条回答
  • 2021-01-31 12:15

    Well there are a couple of problems. The first one is that those two containers (wordpress and wordpress-cli) don't share a volume. So while wordpress has a wordpress installation ready, the wordpress-cli doesn't.

    So you can add volumes to both containers, and then wordpress-cli will find the wordpress installation.

    Then there's a second problem: the wordpress:latest and wordpress:cli images both run with the user www-data, but the problem is that the individual www-data users have different user-id's:

    $ docker run --rm wordpress:latest grep www-data /etc/passwd 
    www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
    $ docker run --rm wordpress:cli grep www-data /etc/passwd   
    www-data:x:82:82:Linux User,,,:/home/www-data:/bin/false
    

    It seems they aren't exactly compatible here. So if you use a shared volume you have to make sure they both use the same user-id. I solved this by having the wordpress:cli run with the user xfs which also has the user id 33.

    The last problem is that your containers have dependencies on each other. Wordpress needs a running MySQL instance and the wordpress-cli needs also the MySQL and the Wordpress to be ready. To make sure MySQL is ready for the wordpress cli installation you either use something like "wait-for-it" or in a simple case you can just wait a couple of seconds and then try it.

    I have tested all those changes and came up with the following docker-compose.yml. I have annotated all the changes I've made with "vstm":

    version: "3.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
    
      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/sh -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
          '
        # vstm: add shared volume
        volumes:
          - wp_data:/var/www/html
    
    volumes:
      db_data:
      # vstm: add shared volume
      wp_data:
    

    It uses a docker-volume but you can also map it to a filesystem. Depends on how you plan to use your docker-compose.

    0 讨论(0)
  • 2021-01-31 12:16

    This one worked for me:

      wpcli:
        depends_on: 
          - mysql
          - wordpress
        image: wordpress:cli
        links:
          - mysql:db
        entrypoint: wp
        command: "--info"
        container_name: ${COMPOSE_PROJECT_NAME}_wpcli
        volumes:
          - ${WORDPRESS_DATA_DIR:-./wordpress}:/var/www/html
        working_dir: /var/www/html
    

    Note that in the line:

    links:
      - mysql:db
    

    mysql = name of my service db = alias name I gave it, can be anything

    Then you issue run wp like so:

    docker-compose run --rm wpcli WORDPRESS_COMMAND

    Source: https://medium.com/@tatemz/using-wp-cli-with-docker-21b0ab9fab79

    0 讨论(0)
提交回复
热议问题