如何在docker-compose.yml中运行wp cli

时间:2018-06-23 09:34:30

标签: wordpress docker docker-compose dockerfile

只需在docker此处开始

所以我在 docker-compose.yml

中找到了这个
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

  wordpress-cli:
    depends_on:
      - db
      - wordpress
    image: wordpress:cli
    command: wp core install --path="/var/www/html" --url=localhost --title="Local Wordpress By Docker" --admin_user=admin --admin_password=secret --admin_email=foo@bar.com

volumes:
  db_data:

所以我想运行 wp核心安装,这样就不必进行手动设置测试Wordpress网站的过程。

但是,当我运行 docker-compose up 时,这似乎不起作用,我在控制台上收到了此错误

enter image description here

我在这里想念什么?任何人都可以帮助我实现自动设置wordpress安装的目标?

预先感谢

3 个答案:

答案 0 :(得分:11)

好几个问题。第一个是这两个容器(wordpresswordpress-cli)不共享卷。因此,尽管wordpress已准备好安装wordpress,但wordpress-cli却没有。

因此您可以将卷添加到两个容器中,然后wordpress-cli将找到wordpress安装。

这是第二个问题:wordpress:latestwordpress:cli图像都与用户www-data一起运行,但是问题在于各个www-data用户具有不同的用户- ID:

$ 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

似乎它们在这里并不完全兼容。因此,如果您使用共享卷,则必须确保它们都使用相同的用户ID。我通过让wordpress:cli与用户xfs一起运行来解决了这个问题,该用户也具有用户ID 33。

最后一个问题是您的容器彼此依赖。 Wordpress需要一个正在运行的MySQL实例,而wordpress-cli也需要MySQL和Wordpress准备就绪。要确保MySQL已准备好安装wordpress cli,您可以使用"wait-for-it"之类的东西,或者在简单的情况下,您可以等待几秒钟然后尝试。

我已经测试了所有这些更改,并提出了以下docker-compose.yml。我已经注释了我对“ 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:

它使用docker-volume,但您也可以将其映射到文件系统。取决于您打算如何使用docker-compose。

答案 1 :(得分:2)

这个对我有用:

  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

注意这一行:

links:
  - mysql:db

mysql = 我的服务名称 db = 我给它的别名,可以是任何东西

然后你像这样发出 run wp :

docker-compose run --rm wpcli WORDPRESS_COMMAND

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

答案 2 :(得分:1)

这是我在 Stack Overflow 的第一个回答:">

实际上,您的问题启发了我,@vstm 的回答对我有所启发。

你可以试试我的一段代码:

1.wait-for-mysql.sh

#!/bin/bash -e

HOST=$(echo $WORDPRESS_DB_HOST | cut -d: -f1)
PORT=$(echo $WORDPRESS_DB_HOST | cut -d: -f2)

CMD=$@

until mysql -h $HOST -P $PORT -D $WORDPRESS_DB_NAME -u $WORDPRESS_DB_USER -p$WORDPRESS_DB_PASSWORD -e '\q'; do
  >&2 echo "Mysql is unavailable - sleeping..."
  sleep 2
done

>&2 echo "Mysql is up - executing command"
exec $CMD

2.compose.yml

version: '3.9'

services:

  wordpress:
    image: wordpress:5.7.0-php7.4-apache
    ports:
      - "80:80"
    volumes:
      - ./wp-data/:/var/www/html/
    networks:
      wp-net: {}

  wp-cli:
    image: wordpress:cli-2.4.0-php7.4
    depends_on:
      - wordpress
    volumes:
      - ./wait-for-mysql.sh:/wait-for-mysql.sh
      - ./wp-data:/var/www/html/ # shared with wordpress service
    user: "33"
    command: >
      /wait-for-mysql.sh
      wp core install
      --path="/var/www/html"
      --url="http://your-url-here"
      --title=your-title-here
      --admin_user=your-user-here
      --admin_password=your-password-here
      --admin_email=your-email-here}
      --skip-email
    networks:
      wp-net: {}

networks:
  wp-net: {}

供您参考: