我是Docker的新手。我知道那里有很多答案。我尝试了此链接host to container,但是我无法解决该问题。我正在使用WordPress CLI图像为WordPress创建一个Docker。
这里是:
version: '3.1'
services:
wordpress:
depends_on:
- db
image: wordpress:latest
volumes:
- wordpress_files:/var/www/html
ports:
- "8080:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: user
WORDPRESS_DB_PASSWORD: password
WORDPRESS_DB_NAME: wordpressdb
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_RANDOM_ROOT_PASSWORD: 1
MYSQL_DATABASE: wordpressdb
MYSQL_USER: user
MYSQL_PASSWORD: password
volumes:
wordpress_files:
db_data:
在上面的代码中,我使用的是与MySQL连接且已成功创建的官方WordPress映像。接下来,我想在该WordPress映像中安装WordPress cli。这是我发现的用于安装WordPress cli的命令。
echo "Installing WP-CLI"
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
chmod +x wp-cli.phar
mv wp-cli.phar /usr/local/bin/wp
我通过将以上命令放入Docker文件的command部分中,尝试了直接方法。但是失败了。因此,我将内容保存在主机的install.sh
文件中。
接下来,我想将文件传输到WordPress映像,并且应该在安装WordPress映像后触发该文件,并且应该使用该文件在该映像中安装cli
。
这是我修改的代码:
version: '3.1'
services:
wordpress:
depends_on:
- db
image: wordpress:latest
volumes:
- wordpress_files:/var/www/html
ports:
- "8080:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: user
WORDPRESS_DB_PASSWORD: password
WORDPRESS_DB_NAME: wordpressdb
COPY /files/install.sh /var/www/html/ =>modified
command: =>modified
/var/www/html/files/install.sh =>modified
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_RANDOM_ROOT_PASSWORD: 1
MYSQL_DATABASE: wordpressdb
MYSQL_USER: user
MYSQL_PASSWORD: password
volumes:
wordpress_files:
db_data:
错误:
ERROR: yaml.scanner.ScannerError: while scanning a simple key
in "./word_press_docker_file.yml", line 18, column 6
could not find expected ':'
in "./word_press_docker_file.yml", line 19, column 6
但是它再次失败。我尝试了几个Stack Overflow答案,但无法弄清楚。我尝试了COPY
命令,但失败了。
答案 0 :(得分:1)
实现此目标的一种方法是通过Dockerfile。
针对您的案例的示例Dockerfile可能如下所示:
FROM wordpress:latest
RUN cd /tmp && echo "Installing WP-CLI" && curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar && chmod +x wp-cli.phar && mv wp-cli.phar /usr/local/bin/wp
然后,您可以将其构建为新的自定义图像,并在您的YAML文件中使用。
编辑:
不再允许使用本地图像。因此,您可以使用 docker标记 命令标记图像,然后在YAML文件中使用。
您可以在此处了解更多信息: https://docs.docker.com/develop/develop-images/dockerfile_best-practices/
这里提到了另一种方法: https://hub.docker.com/_/wordpress/
This image variant does not contain WordPress itself, but instead contains WP-CLI.
The simplest way to use it with an existing WordPress container would be something similar to the following:
$ docker run -it --rm \
--volumes-from some-wordpress \
--network container:some-wordpress \
wordpress:cli user list
Generally speaking, for WP-CLI to interact with a WordPress install, it needs access to the on-disk files of the WordPress install, and access to the database (and the easiest way to accomplish that such that wp-config.php does not require changes is to simply join the networking context of the existing and presumably working WordPress container, but there are many other ways to accomplish that which will be left as an exercise for the reader).
答案 1 :(得分:0)
您可以尝试使用RUN指令,如下所示:
RUN echo "Installing WP-CLI" \
&& curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar \
&& chmod +x wp-cli.phar \
&& mv wp-cli.phar /usr/local/bin/wp