There are seemingly similar questions here (docker-compose volumes_from equivalent with version 3, How to replace volumes_from in docker-composer v3) but I don't think they answer the question (or at least I don't understand how the answers solve the problem). So let me try to ask again, very specifically.
I have this v2 docker-compose.yml:
version: '2'
services:
full-tests:
volumes:
- ..:/opt/project
- ../../../ext-libs:/opt/ext-libs
- ./third-mapping:/opt/third
unit-tests:
volumes_from: full-tests
The point is that the set of volumes is defined once and I can easily reuse them using volumes_from
.
How would you rewrite this in v3?
You could use extension fields to keep the code short but it isn't quite the same as
volumes_from
. For example:I got docker-compose version 3 working with unison. Basically had to replace "volumes_from" and create a global "volumes" declaration.
To answer your question - its impossible with v3 - see the section below. v3 shall not be used as a successor ( also a official statement by docker ) it shall be used in "swarm cases".
nevertheless, what you do should do is using named volumes.
You can combine it with host-mount volumes like this
You can simplify this using the long-syntax introduced in 3.2: https://docs.docker.com/compose/compose-file/#long-syntax-2 so you can define the named volume + bind on the host in the docker-compose file example:
or in short as you had
What you cannot do though, putting the long-syntax under the top-level "volumes" definition to give that volume a name and reused it in the volumes section in the services - that is not possible. To do so, you would use a
And then use the "docker volume create" syntax on the cli to create those volumes with a bind option, as outlines above
but you will never get what volumes_from was doing for you here
There is no equivalent of volumes_from in v3, since v3 is not a successor of v2, its an alternative - please see my comment and the sources here https://github.com/rancher/rancher/issues/3316#issuecomment-310889283
To sum it up - volumes_from and volumes have an overlap in the case volumes_from was just used the wrong way / in the wrong field.
a) If you want data to be persisted across stack upgrades ( down + up ), you pick named volumes - and now, if 2+ services needs to share this, just mount this named volume using
volumes:
.b) If you though, do not want the data to persist over stack upgrades ( e.g. because its source code and the image actually includes an upgrades this ) as a in a usual application + httpd/proxy scenario, you will create a anon-volume for this e.g.
/var/www
in the Dockerfile usingVolume /var/www
and then use volumes_from to mount it in thehttpd
service.the main point with b is, that on stack upgrades, the anon volume will be removed ( `docker-compose down removes anon containers, but not named ones ) and thus the upgrade works as expected, you have a new codebase
Trying to do the same with named volumes will give you a huge suprise on the first upgrade, since the code is on a named volume and that will overlay the codebase on the "fresh" image / new container, thus you will run on the old codebase after the upgrade.