We basically have 3 types of volumes or mounts for persistent data:
-
Bind mounts
-
Named volumes
-
Volumes in dockerfiles
Bind mounts are basically just binding a certain directory or file from the host inside the container (docker run -v /hostdir:/containerdir IMAGE_NAME
)
Named volumes are volumes which you create manually with docker volume create VOLUME_NAME
. They are created in /var/lib/docker/volumes
and can be referenced to by only their name. Let’s say you create a volume called “mysql_data”, you can just reference to it like this docker run -v mysql_data:/containerdir IMAGE_NAME
.
And then there’s volumes in dockerfiles, which are created by the VOLUME
instruction. These volumes are also created under /var/lib/docker/volumes
but don’t have a certain name. Their “name” is just some kind of hash. The volume gets created when running the container and are handy to save persistent data, whether you start the container with -v
or not. The developer gets to say where the important data is and what should be persistent.
What should I use?
What you want to use comes mostly down to either preference or your management. If you want to keep everything in the “docker area” (/var/lib/docker
) you can use volumes. If you want to keep your own directory-structure, you can use binds.
Docker recommends the use of volumes over the use of binds, as volumes are created and managed by docker and binds have a lot more potential of failure (also due to layer 8 problems).
If you use binds and want to transfer your containers/applications on another host, you have to rebuild your directory-structure, where as volumes are more uniform on every host.