Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to initialize a named volume shared across several containers with docker-compose

I'm trying to build my own wordpress-nginx-php_fpm stack with docker-compose but I face a problem with named-volume and its initialization.

Here is my 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: {}

As you can see, I have 2 named volumes. No problem with "wp-db" because it's used only with the "db" service.

My problem is with the "wp-files" volume, mounted in 2 services(=containers)

  • Which service will copy its data to the named volume first ?
  • 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.
  • Am I obliged to use other stuff (like data container) instead of named volume ?

Thank you. Note: (everything is on the same physical host)

like image 323
toussa Avatar asked Oct 30 '22 19:10

toussa


1 Answers

Here is what I could find as an answer:

Which service will copy its data to the named volume first ?

The container that starts first (thanks to volumes-from, depends-on, ...)

Does the second container will overwrite the data put by the first one?

No, once a named volume has been "initialized" (means no empty anymore), it will overwrite every mount point where it 's attached to.

How to "initialize" the named volume somewhere and just use it (after) the 2 containers where its mounted ? I heard about a "nocopy" flag.

There is indeed a "nocopy" flag, like in the "docker run" documentation, BUT doesn't seem to work with other flag ("ro" or "rw").

Am I obliged to use other stuff (like data container) instead of named volume ?

Therefore, no.

like image 98
toussa Avatar answered Nov 15 '22 05:11

toussa