Docker has changed the way we package our programs since its emergence in 2013. It allows developers to build cutting edge software with minimal dependencies and makes deployment easier than ever. If you’re an aspiring software engineer looking to master the docker platform, you need to have a good understanding of various docker volume commands. Volumes are a mechanism used by Docker for providing persistent data storage. They provide significant benefits in terms of software development and deployment. We have taken our time today to teach you the basic building blocks of data volumes in Docker.
Essential Docker Volume Command Examples
This guide will teach you the current methodologies used by developers to implement persistent data storage in commercial and open-source projects. You will be able to create your own data volumes and access them effortlessly from your docker containers after completing this guide. If you’re an absolute beginner, we suggest you review our guide on essential docker commands before working with volumes.
1. Creating Docker Volumes
It is very easy to create data volumes in Docker. You will need to use the volume create command of the docker daemon for doing this. Take a quick look at the below example to see how this works.
$ docker volume create testVolume
This command creates a data volume name testVolume that can be used by a specific container or shared among a cluster of containers. You can now mount this volume to a location inside of the container. Once done, it will be very easy to store or access container data from the host machine.
2. Displaying Available Volumes
It is common for development environments to have a large number of data volumes. So, it’s crucial to identify the specific volumes you are working with. Luckily, it’s very easy to list all currently declared data volumes using the volume ls sub-command of the docker daemon.
$ docker volume ls
You will get a list of all docker volumes present in your host by running the above command. It prints out the names of the data volumes as well as the respective volume drivers. The data volumes are stored in a specific portion of the host filesystem, namely /var/lib/docker/volumes/ in Linux.
3. Inspecting Docker Volumes
The volume inspects command of the docker daemon provides us essential information about a particular volume. It displays information like the volume driver, mount point, scope, and labels. The below command demonstrates this using a practical example.
$ docker volume inspect testVolume
The resultant data shows that our testVolume is mounted at the location /var/lib/docker/volumes/testVolume/_data of our host machine. It also displays the date of creation for this information as well as any specified options. It makes troubleshooting data volumes much easier for developers.
4. Deleting Specific Volumes
If you have accumulated unused data volumes, it’s a good idea to remove them. You can free up storage space in the host environment in this way. The following example illustrates how you can delete a single volume using its name attribute.
$ docker volume rm testVolume
So, you can delete a particular data volume by using the volume rm sub-command, followed by the volume name. It will return the name of the deleted volume in your Linux terminal emulator as a confirmation.
5. Deleting Multiple Volumes
Deleting multiple data volumes is also quite straightforward. Simply pass the name of the volumes you want to delete one after another. The below command shows this in action.
$ docker volume rm testVolume newVolume otherVolume
This command will remove the three specified data volumes. Note that we have not created newVolume and otherVolume. They have been used here for demonstration purposes only. So, make sure your data volumes exist in the first place before deleting them.
6. Deleting All Volumes
It is always a good idea to get rid of your data volumes once your containers are moved onto the production environment from your development machine. Luckily, the docker daemon allows developers to remove all available docker volumes using a single command, as illustrated below.
$ docker volume prune
Moreover, this docker volume command is graceful, meaning it will not remove any volumes that are in use by an existing container. Thus, it is very safe to use and helps to free up much-needed space in development environments.
7. Creating Containers with Data Volumes
We have only shown some basic volume operations so far. However, you will need to mount your data volumes to a docker container in most real-life scenarios. The following docker command shows you how to create a docker container and mount a data volume to this container.
$ docker run -d -it --name test-container -v "testVolume":/tmp ubuntu:xenial
$ docker run -d -it --name test-container --volume "testVolume":/tmp ubuntu:xenial
This command will create a container named test-container using the ubuntu:xenial image and mount our data volume to the /tmp location of this container. You can find more information by running the below command and checking the “Mount” section of the output.
$ docker inspect test-container
8. Mounting Data Volumes to Containers
The –mount option in Docker can be used for addressing an existing data volume to a specific part of the container filesystem. Although the result will be totally similar to the above command, it is more intuitive to many Linux developers.
$ docker run -d -it --name test-container --mount source=testVolume,target=/tmp ubuntu:xenial
The –mount option a set of comma-separated tuples. These are <key>=<value> pairs that denote the attributes of the data volume.
9. Creating Bind Mounts in Docker
Bind mounts are a persistence mechanism that has been available since the early days of Docker. They provide somewhat limited functionality compared to docker volumes but are more suitable in some specific cases. Contrary to volumes, bind mounts depend on the underlying host filesystem.
$ docker run -d -it --name test-container --mount type=bind,source=$(pwd),target=/tmp ubuntu:xenial
The above command creates a bind mount that maps the current working directory of your Linux filesystem to the /tmp location of the container. The type=bind tuple denotes that this is a bind mount, rather than a volume.
10. Pre-Populating Data Volumes
Sometimes developers may require to pre-populate their data volumes when creating docker containers. However, this technique only applies if the target destination of the container holds data prior to the volume creation.
$ docker run -d --name=nginxContainer -v nginxVol:/usr/share/nginx/html nginx:latest
This command first creates the volume nginxVol and populates it with the contents of the /usr/share/nginx/html location of the containers’ filesystem. Now, this data will be accessible to all other containers that share the nginxVol volume. You can use the mount syntax alternatively, as shown below.
$ docker run -d --name=nginxContainer --mount source=nginxVol,destination=/usr/share/nginx/html nginx:latest
11. Using Read Only Volumes
By default, all containers have both read and write access to their respective data volumes. However, not all containers need to write data to a volume. Often simply reading data is good enough. In such cases, you can assign read-only access for your container. Check the docker volume commands to see how this works.
$ docker run -d --name=nginxContainer -v nginxVol:/usr/share/nginx/html:ro nginx:latest
So, by simply adding the ‘:ro‘ field after the destination will prohibit the nginxContainer from writing data to the nginxVol. You will need to use the ‘readonly‘ option if you are creating volumes using the –mount option. Note the difference in syntax carefully.
$ docker run -d --name=nginxContainer --mount source=nginxVol,destination=/usr/share/nginx/html,readonly nginx:latest
12. Creating Volumes using Drivers
Volume drivers are a flexible mechanism used by Docker to provide access to remote mounts, data encryption, and other features. The below commands shows users how to create a docker container using a specific volume driver.
$ docker volume create --driver vieux/sshfs -o [email protected]:/home/session -o password=testPassword sshVolume
This command creates a docker data volume using the vieux/sshfs driver. This driver allows developers to attach remote directories using the SSHFS technology.
13. Running Containers that Creates Volumes using Drivers
You can use the following command to create and start containers that use volume drivers for creating its data volume. This example builds on the above one.
$ docker run -d --name sshfsContainer --volume-driver vieux/sshfs --mount src=sshVolume,target=/tmp,[email protected]:/home/session,volume-opt=password=testPassword nginx:latest
The volume-opt tuple passes the options. It’s not required for every use case. However, if you’re specifying volume-opt, you must use the –mount flag instead of -v or –volume.
14. Creating Services that Use an NFS Volume
NFS or Network File System is a distributed file-sharing system that allows us to use remote filesystems as if they were part of the local filesystem. The following command shows how to create a service that uses an NFS volume.
$ docker service create -d --name nfs-service --mount 'type=volume,source=nfsVolume,target=/tmp,volume-driver=local,volume-opt=type=nfs,volume-opt=device=:/var/docker-nfs,volume-opt=o=addr=10.0.0.10' nginx:latest
This command assumes that our NGS server is running on 10.0.0.10, and it exposes the /var/docker-nfs directory. It also uses NFSv3. So you’ll need to make some adjustments before using this with NFSv4.
15. Backing Up Containers
Volumes provide a flexible way for developers to backup essential container data. For illustration purposes, we will first create a new container named test-container.
$ docker run -v /data --name test-container ubuntu:xenial /bin/bash
So, test-container has a volume called /data. Now, we shall launch another container and mount the /data volume from test-container. We will then mount a local directory of our filesystem as /backup and then store the contents of /data to the /backup directory as backup.tar.
$ docker run --rm --volumes-from test-container -v $(pwd):/backup ubuntu:xenial tar cvf /backup/backup.tar /data
The –volumes-from option denotes that we are actually mounting the data volume of test-container into our new container.
16. Restoring Containers Backups
It is also very easy to restore your containers from backup files. You can restore the data to the same container or to a specific container. We’ll show you how to restore the contents of the backup.tar file created in the earlier example to a different container here.
$ docker run -v /data --name test-container2 ubuntu /bin/bash
This command creates another new container with a /data volume. Now we will extract the contents of the backup.tar file in this new data volume.
$ docker run --rm --volumes-from test-container2 -v $(pwd):/backup ubuntu bash -c "cd /data && tar xvf /backup/backup.tar --strip 1"
You can automate your backups and restorations using these simple yet flexible docker volume commands at ease.
17. Removing Anonymous Volumes
Earlier, we have seen how to delete a normal named volume. However, Docker also has another type of data volume called anonymous volumes. Take a quick look at the below command to see how the deletion operation differs among named and anonymous volumes.
$ docker run --rm -v /anon -v whats-in-a-name:/tmp busybox top
The above command will create an anonymous volume called /anon and a named volume called whats-in-a-name. Now, Docker will remove this container automatically on exit. However, it will only delete the /anon volume, not the whats-in-a-name volume. You need to delete that using the docker volume command rm.
18. Specifying Mount Propagation
Mount propagation denotes the flow of control between the original mount and its replicas. By default, both bind mount and volumes use the rprivate setting. This refrains any propagation between the original mount and its replicas. You can override this setting using the bind-propagation tuple of the bind mount.
$ docker run -d -it --name test-container --mount type=bind,source="$(pwd)"/test,target=/tmp --mount type=bind,source="$(pwd)"/test,target=/temp,readonly,bind-propagation=shared nginx:latest
This command mounts the /test directory twice into the container. Additionally, any new addition to the /tmp mount will be reflected in the /temp mount. However, you can not override the propagation setting when using volumes. It only works when using bind mount on a Linux host.
19. Displaying Volume Command Manual
You can easily view the basic usage of the volume commands by using the below command.
$ man docker volume
20. Displaying Help Page for Sub-Commands
Use the following command to display the primary options available to docker volume.
$ docker volume --help
You can also display more information about a specific option by using the following syntax.
$ docker volume COMMAND --help $ docker volume ls --help
Docker volumes provide a much-needed facility for modern applications. They allow developers to create robust, cutting-edge apps and services by eliminating storage concerns. Moreover, docker volume commands also make it easy to create and maintain backups of your container data. We have laid out this carefully thought guide to help you master data volumes more easily. We highly recommend you start with the basic commands first and then gradually move on to complex, real-life scenarios. Hopefully, we provided you the information you were looking for in this guide. Don’t forget to leave us a comment if you have any questions.