刚刚从这里开始 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 [email protected]
volumes:
db_data:
所以我想运行 wp core install 这样我就不必经历手动设置我的测试 WordPress 站点的过程。
但是,当我运行 docker-compose up 时,这似乎不起作用,我在控制台上收到此错误
我在这里缺少什么?任何人都可以帮助我实现自动化设置 WordPress 安装的目标?
提前致谢
有几个问题。第一个是这两个容器(
wordpress
和 wordpress-cli
)不共享卷。因此,虽然 wordpress
已经准备好安装 WordPress,但 wordpress-cli
还没有。
因此您可以向两个容器添加卷,然后
wordpress-cli
将找到wordpress安装。
然后还有第二个问题:
wordpress:latest
和wordpress: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 [email protected]
'
# vstm: add shared volume
volumes:
- wp_data:/var/www/html
volumes:
db_data:
# vstm: add shared volume
wp_data:
它使用 docker-volume,但您也可以将其映射到文件系统。取决于您计划如何使用 docker-compose。
这个对我有用:
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
这是我在 Stack Overflow 的第一个答案:">
其实你的问题启发了我,@vstm的回答给了我一些指导。
你可以试试我的代码:
1.等待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: {}
供您参考:
为了避免处理已接受答案中提到的不同 UID,可以在 WordPress 容器本身中运行 WP-CLI:(
APT
包是运行二进制文件所必需的,但是,它可能不足以满足所有需要子命令正常工作)
FROM wordpress:php8.3-fpm
COPY --from=wordpress:cli-php8.3 /usr/local/bin/wp /usr/local/bin/wp
RUN apt-get update && \
apt-get install -y --no-install-recommends less mariadb-client sendmail && \
apt-get clean;
SHELL ["bash", "-c"]
ENTRYPOINT \
if [ ! -d /tmp/wordpress ]; then \
install -d -m 0755 /tmp/wordpress/{php-fpm,.wp-cli/cache}; \
chown -R www-data:www-data /tmp/wordpress; \
# Installing the WordPress files by triggering the default entry point
docker-entrypoint.sh php-fpm -v; \
# Avoiding to run the `.bashrc` script when switching the user
export HOME=/tmp/wordpress; \
# Waiting for the MySQL server to start
while ! su -p www-data -s /usr/local/bin/wp db check &>/dev/null; do \
sleep 0.1; \
done; \
# Running the WordPress installation process
su -p www-data -s /usr/local/bin/wp -- core install \
--path=/var/www/html \
--url=localhost \
--title=title \
--admin_email=admin@localhost \
--admin_user=admin \
--admin_password=admin; \
fi; \
exec docker-entrypoint.sh php-fpm