Docker Compose allows you to configure volumes and bind mounts using a short syntax. A few examples:
./public:/usr/share/nginx/html
/var/lib/postgresql/data
/some/content:/usr/share/nginx/html
~/configs:/etc/configs
postgresql:/var/lib/postgresql/data
Which of these are volumes and which are a bind mounts?
Whenever I had to read a docker-compose.yml
file, I had to look up the official documentation or run a quick local experiment to figure out how Docker Compose would mount the directories into the container.
I wrote this article so that next time you read a Docker Compose file, you won’t have to guess anymore. You’ll simply know by looking at the syntax whether a volume or a bind mount is used behind the scenes.
The different variations are essentially three unique forms. I list and explain them in this article below.
volumes
keys in docker-compose.ymlBefore we talk about the different ways to specify volumes, let’s first clarify which volumes
key we’re referring to. In docker-compose.yml
, the volumes
key can appear in two different places.
version: "3.7"
services:
database:
# ...
volumes: # Nested key. Configures volumes for a particular service.
volumes: # Top-level key. Declares volumes which can be referenced from multiple services.
# ...
In this article, we’ll talk about the nested volumes
key. That’s where you configure volumes for a specific service/container such as a database or web server. This configuration has a short (and a long) syntax format.
The volume configuration has a short syntax format that is defined as:
[SOURCE:]TARGET[:MODE]
SOURCE can be a named volume or a (relative or absolute) path on the host system. TARGET is an absolute path in the container. MODE is a mount option which can be read-only or read-write. Brackets mean the argument is optional.
This optionality leads to three unique variations you can use to configure a container’s volumes. Docker Compose is smart about recognising which variety is used and whether to use a volume or bind mount.
#docker #syntax #volume #bind mount