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.
Related
I've got a docker-container that's running a program which outputs something. I want that something to be stored on my local file system, not in a volume. Bind mounts are what I need.
I have a single folder called oniongen and this is where I want the output.
My compose file looks like this:
version: '3'
services:
oniongen:
image: nwtgck/mkp224o
volumes:
- ./oniongen:/gen
command: >
sh -c "mkp224o abcd -d gen"
However, the outputs never reach my local file system.
I've tried
volumes:
oniongen:
driver: local
and this incarnation just in case
volumes:
oniongen:
and also specifying bind under volume type
services:
oniongen:
image: nwtgck/mkp224o
volumes:
- type: bind
source: ./oniongen
target: /gen
volumes:
oniongen:
I've tried other suggestions too and read the docs, but can't seem to get the output stored locally.
If I sh into the container I can see the gen folder and the files in it.
How do I get these files to be stored on my local system?
You're really over-complicating it, just mount a local folder as your volume:
services:
oniongen:
image: nwtgck/mkp224o
volumes:
- ./oniongen:/gen
No need for the volumes top level declaration either.
This will result in everything the container puts in its /gen folder appearing in the host's ./oniongen folder and vice-versa.
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
I don't understand the syntax of the docker-compose-file.
First of all
version: '3'
services:
bla:
command: /bin/bash
stdin_open: true
#tty: true
container_name: docker-gulp-template
#restart: always
build: .
ports:
- '80:3000'
volumes:
- ".:/usr/src/html/bla-source"
volumes:
volumes-xyz:
If I execute it with
docker-compose up
It does create a container with the name
docker-gulp-template_bla
But that sounds illogical to me, though. Shouldn't the container be called this way:
bla_docker-gulp-template ?
Why was it solved like this? Does any of you have an example?
And another point:
volumes:
- ".:/usr/src/html/bla-source"
volumes:
volumes-xyz:
Why do I need the second volumes command and how does docker know that the first volume path belongs to the other volumes name?
Thanks in advance
You don't need the volume section.
A volume can be a named volume, created under the top level volumes section, like
volumes:
volumes-xyz:
and mounted under a service with
volumes:
- "volumes-xyz:/usr/src/html/bla-source"
Named volumes are managed by docker (/var/lib/docker/volumes/ on Linux).
Volume can also be anonymous by
volumes:
- "/usr/src/html/bla-source"
- ".:/usr/src/html/bla-source", on the other hand, creates a "bind mount". It's very similar to volume but you can choose its path to create a two-way mapping between your container and the host.
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.
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