tegradite
tegradite

Reputation: 69

Docker mounting on volume for multiple services

I have a docker-compose file where I define nginx, php, flask and each have volumes like this:

version: '3'

services:
  flask:
   ...
  volumes:
   ./foo:/foo/bar
  flask:
   ...
  volumes:
   ./foo:/foo/bar
  flask:
   ...
  volumes:
   ./foo:/foo/bar

So, my question is obvious. Is there a way to define one volume for the 3 services ONLY? There are other services that don't share the same volume, so is it possible to group services and give them one volume definition?

Upvotes: 1

Views: 498

Answers (1)

Pablo Anaquín
Pablo Anaquín

Reputation: 151

 version: '3'

    services:
      flask1:
       .........
        volumes:
        - ./foo1:/foo1/bar
        - ./foo2:/foo2/bar
        - ./foo3:/foo3/bar

      flask2:
       .........
       volumes_from:
         - flask1

      flask3:
       .........
        volumes_from:
          - flask1

You can put several volumes in one service and then in another service invoke all of them. In the declaration of each service you must indicate the volumes you need, so you will always have to place at least two lines to declare them. Bye.

Upvotes: 3

Related Questions