Docker Named Volumes - docker

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.

Related

How to find all unnamed modules

I got docker compose:
version: '2'
services:
elasticsearch:
image: 'elasticsearch:7.9.1'
environment:
- discovery.type=single-node
ports:
- '9200:9200'
- '9300:9300'
volumes:
- /var/lib/docker/volumes/elastic_search_volume:/usr/share/elasticsearch/data:rw
When I run:
docker volume ls
I see no results. How to list unnamed volumes?
docker volume ls as you've shown it will list all of the volumes that exist.
However, in the docker-compose.yml file you show, you're not creating a named or anonymous volume. Instead, you're creating a bind mount to connect a host directory to the container filesystem space. These aren't considered "volumes" in a technical Docker sense, and a docker volume command won't show or manipulate those.
Reaching directly into /var/lib/docker usually isn't a best practice. It's better to ask Docker Compose to manage the named volume for you:
version: '2'
services:
elasticsearch:
volumes:
# No absolute host path, just the volume name
- elastic_search_volume:/usr/share/elasticsearch/data:rw
volumes:
elastic_search_volume:
# Without this line, Compose will create the volume for you.
# With this line, Compose expects it to already exist; you may
# need to manually `docker volume create elastic_search_volume`.
# external: true

How to specify volume host path in docker-compose?

I want to share a volume among multiple containers, and specify the path for this volume on the host.
I used the following settings:
version: '3'
services:
service1:
image: image1
volumes:
- volume1:/volume1
service2:
image: image2
volumes:
- volume1:/volume1
volumes:
volume1:
driver: local # meaning?
driver_opts:
o: bind # meaning?
type: none # meaning?
device: /volume1 # the path on the host
But I am not sure of the driver: local, type: none and o: bind options.
I would like to have a regular volume (like without specifying any driver nor driver_opts), just being able to specify the path on the host.
You're looking for a bind mount. Specifying the volumes key means that you're creating a volume in the Docker machine for persistent storage. Despite the name, a volume is not necessarily related to volumes.
Use something like:
version: '3'
services:
service1:
image: image1
volumes:
- type: bind # Host and Docker machines have identical views to the same directory; changes propagate both ways
source: . # Host machine directory to mount
target: /app # Docker machine directory to be mapped to

Docker compose: meaning of {} in volume definition

What is the meaning of {} in volume definition?
For example
version: '2'
volumes:
dataelasticsearch: {}
services:
elasticsearch:
image: docker.elastic.co/elasticsearch/elasticsearch:5.4.3
volumes:
- ./dataelasticsearch:/usr/share/elasticsearch/data
It's just an empty mapping. It just means that no extra options were given to the named volume.
From the tests I have done, this is no different from leaving it blank like:
volumes:
dataelasticsearch:
The docker-compose docs do not give any more insights into this.
One more thing: you are defining a named volume at the top but then you are binding a mounted volume in the service:
volumes:
- ./dataelasticsearch:/usr/share/elasticsearch/data
Here ./dataelasticsearch basically creates a folder in your local directory which is mounted as a volume. If you want this feature, you do not need the named volume at all.

How to set a path on host for a named volume in docker-compose.yml

Example below creates dbdata named volume and references it inside db service:
version: '2'
services:
db:
image: mysql
volumes:
- dbdata:/var/lib/mysql
volumes:
dbdata:
driver: local
(from https://stackoverflow.com/a/35675553/4291814)
I can see the path for the volume defaults to:
/var/lib/docker/volumes/<project_name>_dbdata
My question is how to configure the path on host for the dbdata volume?
With the local volume driver comes the ability to use arbitrary mounts; by using a bind mount you can achieve exactly this.
For setting up a named volume that gets mounted into /srv/db-data, your docker-compose.yml would look like this:
version: '2'
services:
db:
image: mysql
volumes:
- dbdata:/var/lib/mysql
volumes:
dbdata:
driver: local
driver_opts:
type: 'none'
o: 'bind'
device: '/srv/db-data'
I have not tested it with the version 2 of the compose file format, but https://docs.docker.com/compose/compose-file/compose-versioning/#version-2 does not indicate, that it should not work.
I've also not tested it on Windows...
The location of named volumes is managed by docker; if you want to specify the location yourself, you can either "bind mount" a host directory, or use a volume plugin that allows you to specify a path.
You can find some details in another answer I posted recently; https://stackoverflow.com/a/36321403/1811501

Volumes and docker-compose

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

Resources