问题
Using docker-compose I'm attempting to create a shared volume that two of my services can both use. I have the following in my docker-compose.yml
version: '2'
volumes:
bundler:
driver: local
sidekiq:
build: .
volumes_from:
- bundler:/.bundle
web:
build: .
volumes_from:
- bundler:/.bundle
This however doesn't work and gives me:
ERROR: Service "sidekiq" mounts volumes from "bundler", which is not the name of a service or container.
What is the correct way of doing this using docker-compose?
回答1:
I think you mixed two concepts here.
volumes_fromis used when one container has some volumes (mounted or not) that should be used for storage. You can then use this container's volumes by referencing the so calleddata-only-containerwithvolumes_fromwhich must point to a container or (compose) service name.volumeson the other hand is used to reference either a local folder or a named volume. A named volume then must be declared in the top level entryvolumesof the compose file as you already did.
So in your case, a switch from volumes_from to volumes should do the trick. Please see the reference docs for details: https://docs.docker.com/compose/compose-file/#/volumes-volume-driver
回答2:
The answer should be
version: '2'
volumes:
bundler:
driver: local
sidekiq:
build: .
volumes:
- bundler:/.bundle
web:
build: .
volumes_from:
- sidekiq:rw
So sidekiq is you so called data-container, exposing and creating the bundler volume. Then you mount this volume in all other containers you need it, in your case for now web. Hope this helps
回答3:
The "volumes_from" references a container. Since you're using a named volume, you need "volumes":
version: '2'
volumes:
bundler:
driver: local
services:
sidekiq:
build: .
volumes:
- bundler:/.bundle
web:
build: .
volumes:
- bundler:/.bundle
Also, since you're using version 2, you need to specify services in their own services: section instead of the top level of the yml.
回答4:
If you really want to use volumes_from with a container(data only), use
volumes_from:
- container:"yourdataonlycontainer"
来源:https://stackoverflow.com/questions/38984226/service-x-mounts-volumes-from-y-which-is-not-the-name-of-a-service-or-container