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

How to initialize a named volume shared across several containers with 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 个服务(=容器)中

谢谢。 注意:(一切都在同一台物理主机上)

以下是我能找到的答案:

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?

不,一旦命名卷"initialized"(意味着不再是空的),它将覆盖它附加到的每个挂载点。

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 ?

因此,没有。