When configuring a docker-compose.yml
, I can easily mount a volume that maps a folder from the host machine to the container:
...
volumes:
- "/host/folder/:/container/folder/"
...
However, if I try to use the long syntax, that doesn’t work anymore:
...
volumes:
- type: volume
source: /host/folder
target: /container/folder/
...
According to the docs
source: the source of the mount, a path on the host for a bind mount,
or the name of a volume defined in the top-level volumes key. Not
applicable for a tmpfs mount.
So, it seems that in the long syntax I have to use the bind type to mount a host path. Does this make sense, different features according to syntax?
Furthermore, again, according to the docs,
Volumes are the preferred mechanism for persisting data generated by
and used by Docker containers. While bind mounts are dependent on the
directory structure and OS of the host machine, volumes are completely
managed by Docker.
So, I much prefer having volumes instead of binds. Do I have to use the short syntax for that?
2
Answers
Try this:
The
type:
field says whether it’s a namedvolume
, abind
mount, or a couple of other things. Since you’re mounting a host directory, you need to specifytype: bind
in the extended syntax.IMHO the Docker documentation is very enthusiastic about named volumes and glosses over their downsides. Since you can’t access the contents of a named volume from outside of Docker, they’re harder to back up and manage, and a poor match for tasks like injecting config files and reviewing logs. I would not automatically reach for a named volume because the Docker documentation suggests it’s preferred.