我正在尝试使用 docker-compose 构建我自己的 wordpress-nginx-php_fpm 堆栈,但我遇到了命名卷及其初始化的问题。
这是我的 docker-compose.yml:
version: '2'
services:
db: #https://hub.docker.com/_/mysql/
image: mysql
restart: always
volumes:
- "wp-db:/var/lib/mysql:rw"
- env_file:
- "./conf/db/mysql.env"
networks:
- back
nginx: #https://hub.docker.com/_/nginx/
image: nginx
restart: always
volumes:
- "wp-files:/usr/share/nginx/html"
- "./conf/nginx:/nginx:ro"
- "./conf/tools:/tools:ro"
networks:
- front
- back
ports:
- "8080:80"
environment:
- "PHP_FPM_HOST=php-wp:9000"
- "PHP_FPM_ROOT_DIR=/var/www/html"
command: "bash /tools/wait-for-it.sh php-wp:9000 -t 30 -- bash /tools/detemplatize-it.sh /nginx/nginx.template:/nginx.conf -- nginx -c /nginx.conf"
php-wp: #https://hub.docker.com/_/wordpress/
image: "wordpress:fpm"
restart: always
volumes:
- "wp-files:/var/www/html"
- "./conf/tools:/tools:ro"
env_file:
- "conf/wp/wordpress.env"
networks:
- back
command: "bash /tools/wait-for-it.sh db:3306 -t 30 -- php-fpm -F"
networks:
front: {}
back: {}
volumes:
wp-files: {}
wp-db: {}
如您所见,我有 2 个命名卷。 “wp-db”没有问题,因为它只与“db”服务一起使用。
我的问题是“wp-files”卷,安装在 2 个服务(=容器)中
- 哪个服务会首先将其数据复制到命名卷?
- 第二个容器会覆盖第一个容器放入的数据吗?
- 如何在某处“初始化”命名卷并仅在其安装的 2 个容器(之后)使用它?我听说有一个“nocopy”标志。
- 我是否必须使用其他东西(如数据容器)而不是命名卷?
谢谢。 注意:(一切都在同一台物理主机上)
最佳答案
这是我能找到的答案:
Which service will copy its data to the named volume first ?
首先启动的容器(感谢 volumes-from,depends-on,...)
Does the second container will overwrite the data put by the first one?
不,一旦一个命名卷被“初始化”(意味着不再是空的),它将覆盖它附加到的每个挂载点。
How to "initialize" the named volume somewhere and just use it (after) the 2 containers where its mounted ? I heard about a "nocopy" flag.
确实有一个“nocopy”标志,就像在“docker run”文档中一样,但似乎不适用于其他标志(“ro”或“rw”)。
Am I obliged to use other stuff (like data container) instead of named volume ?
因此,没有。
关于php - 如何使用 docker-compose 初始化跨多个容器共享的命名卷,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39638552/