I'm trying to create a docker-compose.yml file that contains a --volumes-from instruction. Does anyone know the syntax?
I have been looking online for some time now, and it appears that the --volumes-from command is only available as a docker command. I hope I'm wrong.
Aug. 2022:
brandt points out in the comments to the updated docker-compose documentation.
Note August 2017: with docker-compose version 3, regarding volumes:
The top-level volumes key defines a named volume and references it from each service’s volumes list.
This replaces volumes_from in earlier versions of the Compose file format. See Use volumes and Volume Plugins for general information on volumes.
Example:
version: "3.2"
services:
web:
image: nginx:alpine
volumes:
- type: volume
source: mydata
target: /data
volume:
nocopy: true
- type: bind
source: ./static
target: /opt/app/static
db:
image: postgres:latest
volumes:
- "/var/run/postgres/postgres.sock:/var/run/postgres/postgres.sock"
- "dbdata:/var/lib/postgresql/data"
volumes:
mydata:
dbdata:
This example shows a named volume (mydata) being used by the web service, and a bind mount defined for a single service (first path under db service volumes).
The db service also uses a named volume called dbdata (second path under db service volumes), but defines it using the old string format for mounting a named volume.
Named volumes must be listed under the top-level volumes key, as shown.
February 2016:
The docs/compose-file.md mentions:
Mount all of the volumes from another service or container, optionally specifying read-only access(ro) or read-write(rw).
(If no access level is specified, then read-write will be used.)
volumes_from:
- service_name
- service_name:ro
- container:container_name
- container:container_name:rw
For instance (from this issue or this one)
version: "2"
services:
...
db:
image: mongo:3.0.8
volumes_from:
- dbdata
networks:
- back
links:
- dbdata
dbdata:
image: busybox
volumes:
- /data/db
Related
I have this docker-compose.yml
version: 2.4
services:
foo:
image: ubuntu:focal
container_name: oof
volumes:
- ./foo:/foo
bar:
image: ubuntu:focal
container_name: rab
volumes_from:
- container:oof
With compose 1.29.2 and docker 20.10.8. But it does not start.
I get an error
ERROR: Service "bar" mounts volumes from "oof", which is not the name of a service or container.
How do I properly reference volumes by container name in compose?
The best way is to explicitly spell out the volumes you're reusing. If the foo image has VOLUME directives, you'll also mount the automatically-created anonymous volumes; you also have no control over where the volumes will be mounted, and if the two containers aren't similar enough, there could be conflicts. (This is essentially the logic behind Compose file version 3 removing volumes_from:.)
version: 2.4
services:
foo:
volumes:
- ./foo:/foo
bar:
volumes:
- ./foo:/foo # or a different container path if that suits your needs
In general in Compose, you shouldn't need to manually specify container_name:. Compose will generate unique container names, within the docker-compose.yml file if you need to refer to things you can use the service name, and there are docker-compose wrapper commands that know how to find the right container. If you do need to use the older volumes_from: syntax use the service name instead:
version: '2.4' # does not work in version 3
services:
foo:
image: ...
volumes:
- ./foo:/foo
# no container_name:
bar:
image: ...
volumes_from: foo
I'm starting to learn about docker and I want to run a nextcloud using the docker-compose file.
The docker-compose file I am using. I only specified the volumes which are folders I have created.
version: ‘2’
volumes:
nextcloud:/home/ahmed/Desktop/docker-storage/nextcloud
db:/home/ahmed/Desktop/docker-storage/db
services:
db:
image: mariadb
restart: always
volumes:
- db:/var/lib/mysql
environment:
- MYSQL_ROOT_PASSWORD= anha1985
- MYSQL_PASSWORD= anha1985
- MYSQL_DATABASE=nextcloud
- MYSQL_USER=nextcloud
app:
image: nextcloud
ports:
- 8080:80
links:
- db
volumes:
- nextcloud:/var/www/html
restart: always
The error message that I have is.
ERROR: In file ‘./docker-compose.yml’, volume must be a mapping, not a string.
Please advice me.
The volume section at the top of the compose file is for defining named volumes. If you only have host volumes with an absolute or relative path, then you would only define the volume within each service. For a named volume, you would define it both at the top level and within each service that needs access to the named volume. These two sections, while named similarly, have a different syntax.
See this documentation for the volume section at the top level of the yml file: https://docs.docker.com/compose/compose-file/#volume-configuration-reference
And this documentation applies to the volume section within each service: https://docs.docker.com/compose/compose-file/#volumes
What's the right way to mix named volumes with and without local host path in docker compose v3?
This way I'm getting YML error:
volumes:
/mnt/volume-part1:/volume
conf:
vhost:
html:
certs:
Then I'd like to refer to volume inside containers...
For named volumes, you need to declare the volume name under the dedicated volumes section in the compose file. For a mount, you don't declare it in that section:
Consider the following compose file:
version: "3"
services:
db:
image: db
volumes:
- data-volume:/var/lib/db
- /mnt/volume-part1:/volume
volumes:
data-volume:
As you can see the named volume data-volume needes to be declared in the volumes section before being assiged to the container.
Whereas the directory mount is directly mounted onto the container.
UPDATE
If you don't want to replicate the machine path on all the container, you can use a clever trick to specify where exactly the named volume will be created as such:
version: "3"
services:
db:
image: db
volumes:
- data-volume:/var/lib/db
- volume-part1:/volume
volumes:
data-volume:
volume-part1:
driver_opts:
type: none
device: /mnt/volume-part1
o: bind
As you can see above, we have created a named volume volume-part1 and specified where this volume will be backuped on the host machine.
I would like to define a general mount volume - along with all the options I would like to have it associated - that can be reused across multiple services. In fact, I'm developing a project which uses the same source for several microservices.
That way, the volume will be simpler to manage and modify.
To start off, I used the old way which took advantage of volumes_from:
shared:
image: phusion/baseimage
volumes:
- ./code:/var/www/html
nginx:
build: docker/nginx
ports:
- "8080:80"
links:
- php
volumes_from:
- shared
This works, but I had to define a shared service to make it work. As of the 3.0 version, volumes can be used, so I would like to define a general volume and use it into my nginx service, but I'm not finding the right syntax:
version: '3.3'
volumes:
vol_test:
type: bind
source: ./code
target: /var/www/html
volume:
nocopy: true
services:
nginx:
build: docker/nginx
ports:
- "8080:80"
volumes:
- vol_test
Update
I've found that defining a volume the way I want could not be possible, since the following definition:
volumes:
data-volume:
type: bind
source: ./code
target: /var/www/html
volume:
nocopy: true
will produce this output when calling docker-compose up:
ERROR: The Compose file './docker-compose.yml' is invalid because:
volumes.data-volume value Additional properties are not allowed ('volume', 'source', 'type', 'target' were unexpected)
I guess I still have to use the volumes_from way then. Can anybody confirm that?
I can confirm your observation: If you want to mount a host directory, you'll have to use the bind mount syntax.
You do not have to use volume_from anymore. Form
https://docs.docker.com/compose/compose-file/#volumes:
But, if you want to reuse a volume across multiple services, then
define a named volume in the top-level volumes key. Use named volumes
with services, swarms, and stack files.
They even specifically address your issue:
Note: The top-level volumes key defines a named volume and references it from each service’s volumes list. This replaces
volumes_from in earlier versions of the Compose file format. See Use
volumes and Volume Plugins for general information on volumes.
So, for a MWE I defined an empty volume and referenced it in two services. This is the compose file:
version: '3.3'
volumes:
vol_test:
services:
reader:
image: ubuntu
tty: true
command: bash -c "sleep 1; cat /volume/file;exit 0"
volumes:
- vol_test:/volume
writer:
image: ubuntu
tty: true
command: bash -c "date | tee /volume/file; exit 0"
volumes:
- vol_test:/volume
This gives us the following behavior:
$ date; docker-compose up
So 27. Aug 11:54:13 CEST 2017
Creating network "dockercomposetest_default" with the default driver
Creating dockercomposetest_writer_1 ...
Creating dockercomposetest_reader_1 ...
Creating dockercomposetest_writer_1
Creating dockercomposetest_reader_1 ... done
Attaching to dockercomposetest_writer_1, dockercomposetest_reader_1
writer_1 | 27 09:54:15 UTC 2017
reader_1 | 27 09:54:15 UTC 2017
Gracefully stopping... (press Ctrl+C again to force)
With v2 of docker-compose synthax, we were able to do something like this:
version: '2'
services:
app:
image: tianon/true
volumes:
- ../app:/var/www/app
nginx:
image: nginx
volumes_from:
- app
php:
image: php
volumes_from:
- app
In v3.2 volumes_from is now invalid option. The documentation is all for using new top-level volumes synthax, which is all the ways better.
I've read some comments on github, and the only solution that people propose is
version: '3.2'
services:
nginx:
image: nginx
volumes:
- app:/var/www/app
php:
image: php
volumes:
- app:/var/www/app
volumes:
app:
driver_opts:
type: none
device: ../app
o: bind
Which looks worse obviously, and it even doesn't work for me. It gives me an error: no such file or directory. So what else should I try? It seems like I can still use links instead of top-level volumes, but it's considered as legacy option in documentation. So how to do it right with new syntax?
EDIT:
Question has been identified as a possible duplicate, but I don't agree. See my comment bellow for explanation.
As the topic starter already mentions, volumes_from has been removed from the new docker-compose syntax, according to the documentation in favour of named volumes defined in the top level key volumes. The documentation also states the difference between volumes and bind mounts, one of which is who manages the contents:
By contrast, when you use a volume, a new directory is created within Docker’s storage directory on the host machine, and Docker manages that directory’s contents.
If this is the case, then it does not make sense to bind mount a host folder into a volume and let it be controlled by the host's file system and by Docker simultaneously.
If you still want to bind mount the same folder into two or more containers you could try something like:
version: '3.2'
services:
nginx:
image: nginx
volumes:
- type: bind
source: ../app
target: /var/www/app
php:
image: php
volumes:
- type: bind
source: ../app
target: /var/www/app