php – 如何使用docker-compose初始化跨多个容器共享的命名卷

我正在尝试使用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 ?

首先启动的容器(感谢volume-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 ?

因此,没有.

点赞