A comprehensive guide to Docker Volumes
In the world of containers, and especially in Docker, data persistence is an important topic. Docker containers are ephemeral by nature and storing data inside a container is a really bad idea. To achieve data persistence let’s discuss about Docker Volumes.
What is a Docker (data) Volume?
Put it simply, a Docker Volume is a special folder inside a container that does not go away when a container is deleted. Instead of storing data inside the container, which is a bad idea, you should always use Docker Volumes when you need data persistence. By using volumes you will ensure that data inside them will not be deleted when docker rm is used. Doing so, the container will be removed and all the data within the container will be lost, however volumes will retain all the data. Also volumes do not affect Docker images and will not be included when updating an image.
Docker Volumes types
Now there are essentially three types of volumes:
- Local volumes: these volumes are stored locally on the host the container is running on. They are stored in /var/lib/docker/volumes/ .
- Host directory: these volumes are actually local directories from the host on which the container is running. A link is created between the host’s directory and the path inside the container.
- Plugins: Docker volumes are modular and there are many plugins that enable integration with other protocols (e.g. iSCSI) or solutions (vSphere, NetApp).
Containers can also be named, allowing easier retrieval.
I will assume you have already grasped the Docker basic notions. Now we will create a new container with a local volume:
# docker run --name volume-container -v /usr busybox # docker volume ls DRIVER VOLUME NAME local d17ce525813a7330eec3f6ddc2cc73418cdab7d34e216d2017d80ccfb9378821 # ls -la /var/lib/docker/volumes/d17ce525813a7330eec3f6ddc2cc73418cdab7d34e216d2017d80ccfb9378821/_data/ total 4.0K drwxr-xr-x 2 daemon daemon 4.0K Jan 12 18:10 sbin
The key to create a volume is the -v flag. As you can see from the output of docker volume ls a new volume has been created. This volume is populated with the content of /usr of its container and will now persist even if we were to delete the container. Also you can see that we can access the volume’s content through the host with normal command line utilities.
Shared host directory
Another method to use volumes is to link a directory directly from the host:
# mkdir /hostdirectory # touch /hostdirectory/sharedfile # docker run -it --name sharedvolume-container -v /hostdirectory:/opt busybox /bin/sh / # ls -lh /opt total 0 -rw-r--r-- 1 root root 0 Jan 30 21:30 sharedfile
The -v flag for shared directories has a different syntax: hostdir:containerdir. When a directory from the host (in this case /hostdirectory) is mounted inside a container (in this case /opt) it will not be populated with the content from the container. Also this volume will not be listed by docker volume ls .
In this article we spoke about Docker Volumes, a system created to ensure data persistence within Docker containers. It has also been explained how to create local volumes and shared directories with the host.
Latest posts by mark (see all)
- How to install Fedora 31 in 10 easy steps - 6 November 2019
- How to install Ubuntu 19.10 Eoan Ermine in 10 easy steps! - 30 October 2019
- How to install NextCloud 17 server on CentOS 7.x - 23 October 2019