16

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.

m0onspell
  • 517
  • 5
  • 13
  • Possible duplicate of [How to replace volumes\_from in docker-composer v3](https://stackoverflow.com/questions/42244079/how-to-replace-volumes-from-in-docker-composer-v3) – panK Jul 07 '17 at 11:58
  • 2
    @panK I was asking about native docker way to do the thing which I was able to achieve with v2 synthax. If there's a need for third-party plugin to achieve same effect, then the answer would be probably: it's not possible. But I am not sure that it's actually the case because from what I can see on github repo, suggested plugin is not actively maintained and isn't very popular. At the same time, it's quite common usage case. So I am assuming that author of that question was looking for any possible option, and I am looking for native, "official" docker way. – m0onspell Jul 07 '17 at 20:39
  • I marked it as a possible duplicate, because i went through many post related to 'volumes_from' that day and i believe everyone is searching for native, official and reliable solution. I couldn't find any official one, because it looks like they just abandoned such idea. This is the only reason i stayed with v2 syntax. – panK Dec 05 '17 at 15:10
  • @panK well, then you can post an answer bellow, make some arguments, and if it's really "not possible", then I'll accept this as an aswer. – m0onspell Dec 06 '17 at 15:52

1 Answers1

3

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
Bart Joosten
  • 119
  • 1
  • 5
  • 6
    The question contains word "sharing", not repeating. Sharing was possible in older versions. Repetition is always a bad practice. – m0onspell Dec 06 '17 at 15:50
  • 3
    For programming languages repetition is a bad practice, I agree. This is a configuration file where being explicit can help clear up any ambiguity. By the way, if you had compared my solution with the 3.2 syntax to the 2.0 syntax with docker inspect, you would have seen that under the hood both versions are almost identical, and you would have seen that the same mount is "shared" between containers. – Bart Joosten Jan 03 '18 at 08:13
  • This syntax does not work for me. I get a `service "whatever" refers to undefined volume whatever_volume: invalid compose project` On the other hand the syntax suggested by OP with a volumes: section apart does work – cassepipe Oct 08 '22 at 22:57