Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

docker-compose v3: sharing bind-mounted volume between multiple containers with top-level volumes syntax

With v2 of docker-compose synthax, we were able to do something like this:

version: '2'
services:
  app:
    image: tianon/true
    volumes:
      - ../app:/var/www/app
  nginx:
    image: nginx
    volumes_from:
      - app
  php:
    image: php
    volumes_from:
      - app

In v3.2 volumes_from is now invalid option. The documentation is all for using new top-level volumes synthax, which is all the ways better. I've read some comments on github, and the only solution that people propose is

version: '3.2'
services:
  nginx:
    image: nginx
    volumes:
      - app:/var/www/app
  php:
    image: php
    volumes:
      - app:/var/www/app
volumes:
  app:
    driver_opts:
      type: none
      device: ../app
      o: bind

Which looks worse obviously, and it even doesn't work for me. It gives me an error: no such file or directory. So what else should I try? It seems like I can still use links instead of top-level volumes, but it's considered as legacy option in documentation. So how to do it right with new syntax?

EDIT: Question has been identified as a possible duplicate, but I don't agree. See my comment bellow for explanation.

like image 953
m0onspell Avatar asked Jun 29 '17 12:06

m0onspell


1 Answers

As the topic starter already mentions, volumes_from has been removed from the new docker-compose syntax, according to the documentation in favour of named volumes defined in the top level key volumes. The documentation also states the difference between volumes and bind mounts, one of which is who manages the contents:

By contrast, when you use a volume, a new directory is created within Docker’s storage directory on the host machine, and Docker manages that directory’s contents.

If this is the case, then it does not make sense to bind mount a host folder into a volume and let it be controlled by the host's file system and by Docker simultaneously.

If you still want to bind mount the same folder into two or more containers you could try something like:

version: '3.2'
services:
  nginx:
    image: nginx
    volumes:
      - type: bind
        source: ../app
        target: /var/www/app
  php:
    image: php
    volumes:
      - type: bind
        source: ../app
        target: /var/www/app
like image 67
Bart Joosten Avatar answered Sep 20 '22 08:09

Bart Joosten