Docker volumes vs bind mounts

By default a docker container won’t store any persistent data, so anything that we write to its writable layer won’t be available once we stop it.When running Docker containers we might be interested in persisting certain data. This can be achieved by using either volumes or bind mounts. Below I’m describing main differences between the two.

Bind mounts are directories on the host filesystem mounted onto a Docker container. These can be modified outside of Docker.

Volumes on the other hand are managed by Docker only and you can use Docker cli commands to manage them directly. They are a preferred way to persist data.

There are named volumes created by the “volume” command that can be referenced by their name and there are volumes that are created by the VOLUME instruction from the docker files. The latter are identified by a hash and stored in the same Docker volumes folder.

When mounting volumes you specify the name instead of the paths you would like to mount. Volumes don’t increase the size of docker containers. If you would like to keep your own directory structure you should use bind mounts. Volumes make your project more portable, because with bind mounts you would need to rebuild your directory structure when migrating and you must ensure that all the security settings like user permissions are setup correctly.