如何在 docker-compose.yml 中运行 wp cli [英] How to run wp cli in docker-compose.yml

查看:17
本文介绍了如何在 docker-compose.yml 中运行 wp cli的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

刚从 docker 开始

Just starting in docker here

所以我在我的 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 core install,这样我就不必经历手动设置我的测试 wordpress 网站的过程.

So I wanted to run the wp core install so that I won't have to go through the process of manually setting up my test wordpress site.

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

However when I run docker-compose up, this does not seem to work, I got this error on the console

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

What am I missing here? Anyone can help me accomplish my goal of automating the of setting up wordpress install?

提前致谢

推荐答案

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

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.

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

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

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

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

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

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.

最后一个问题是你的容器相互依赖.Wordpress 需要一个正在运行的 MySQL 实例,并且 wordpress-cli 还需要准备好 MySQL 和 Wordpress.为了确保 MySQL 已准备好安装 wordpress cli,您可以使用 "wait-for-it" 或者在一个简单的情况下,您可以等待几秒钟然后尝试.

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.

我已经测试了所有这些更改并提出了以下 docker-compose.yml.我已经用vstm"注释了我所做的所有更改:

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:

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

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

这篇关于如何在 docker-compose.yml 中运行 wp cli的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆