问题
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
回答1:
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
来源:https://stackoverflow.com/questions/45494746/docker-compose-volumes-from-usage-example