JavaSa
JavaSa

Reputation: 6241

docker-compose volumes_from usage example

Can you please provide an example to sharing a path using volumes_from from container A to Container B, in addition how container B can access this path after sharing is done.

Thanks

Upvotes: 36

Views: 69291

Answers (2)

Samuka Sampaio
Samuka Sampaio

Reputation: 31

version: "3"

services:

  web:
    nginx:alpine
    ports:
    - "80:80"

  postgres:
    image: postgres:9.4
    volumes:
      - db-data:/var/lib/db

  backup:
    image: postgres:9.4
    volumes:
      - db-data:/var/lib/backup/data

  redis:
    image: redis
    ports:
      - "6379:6379"
    volumes:
      - ./data:/data

volumes:
  db-data:
    external: true

For me what solved was adding an external volume not created by compose using "external: true" https://docs.docker.com/compose/compose-file/compose-file-v3/#external

Upvotes: 3

julian salas
julian salas

Reputation: 4360

As documentation said volumes if you are in version 3 you can use The top-level volumes to define a named volume as db-data ee code below and you can reference it in every services something like this:

version: "3"

services:

  web:
    nginx:alpine
    ports:
    - "80:80"

  postgres:
    image: postgres:9.4
    volumes:
      - db-data:/var/lib/db

  backup:
    image: postgres:9.4
    volumes:
      - db-data:/var/lib/backup/data

  redis:
    image: redis
    ports:
      - "6379:6379"
    volumes:
      - ./data:/data

volumes:
  db-data:

version 2.0:

volumes_from allow you mount all data or volume from another service or container, you have to specify the access level how documentation said volumes from in your code you can use something like this:

version: "2"

services:
  web:
    image: nginx:alpine
    ports:
      - "80:80"
    volumes_from:
      - redis:rw
  postgres:
    image: postgres:9.4
    volumes:
      - /data/webapp
  backup:
    image: postgres:9.4
    volumes:
      - /var/lib/backup/data

  redis:
    image: redis
    ports:
      - "6379:6379"
    volumes:
      - /data/db

To code above redis define a volume services and then you can use in another container for example web with volumes_from look like web service use that volume service specify access level to read and write

Upvotes: 42

Related Questions