Named volume is a new feature since Docker 1.9, it allows you to manage persistent data more easily, and also the volume can be provided by other storage drivers such as Glusterfs instead of “just a directory on the host”. This new feature is particularly useful in production environment. (On the Dev side, I think we still need mounted volume)
Getting Started
First, you have to install Docker 1.9 or later, check the version:
1 2 | $ docker -v Docker version 1.10.2, build c3959b1 |
To check that you really has the named volume feature, try this command, you will see something like this:
1 2 3 4 | $ docker volume ls DRIVER VOLUME NAME local my-lovely-volume local my-precious-data |
Create some named volumes
To create a new named volume named “my-precious-data” by docker run:
1 | $ docker run -v my-precious-data:/data -p 8000:80 webapp:latest --name webapp |
To create a new Mariadb instance that will persist data:
1 | $ docker run -v dbdata:/var/lib/mysql -p 3306:3306 mariadb:latest --name db |
Some Notes
Some behaviour not particularly well explained in the doc:
- If you create a named volume by running a new container from image by docker run -v my-precious-data:/data imageName, the data within the image/container under /data will be copied into the named volume.
- If you create another container binds to an existing named volume, no files from the new image/container will be copied/overwritten, it will use the existing data inside the named volume.
- They don’t have a docker command to backup / export a named volume. However you can find out the actual location of the file by “docker volume inspect [volume-name]”.
- A more fancy way to backup/restore a named volume is to run a container (could be from a simple debian:jessie image) that binds to the named volume and run a command to export to a host directory / export to the cloud. Then later import by the same method.Backup:1$ docker run --rm --volumes-from dbstore -v $(pwd):/backup ubuntu tar cvf /backup/backup.tar /dbdata
Restore:1$ docker run --rm --volumes-from dbstore2 -v $(pwd):/backup ubuntu bash -c "cd /dbdata && tar xvf /backup/backup.tar --strip 1"
This is all I know at the moment, more usage and examples in the future!
3 replies on “Docker named volume explained”
Excellent introduction.
Thanks, even in almost 2019 these few sentences help with an understanding of the docker named volumes.
Very helpful, thanks!