Introduction
In earlier posts, we created a script in our container and we were able to execute it. But when the container is removed the script will get removed right along with it. What if we wanted the files created during our container lifecycle to persist after the container has been terminated? We could do so by using docker volumes which are the preferred mechanism for allowing data persistence in Docker containers.
Advantages of docker volumes:
- They can be used on both Linux and Windows containers.
- We can use volume drivers to store data on a remote host or even a cloud provider.
- New volumes can have their content pre-populated by a container.
We’ll now walk you through a step by step process to create a docker volume and mount it in a container.
Step 1: Create a docker volume.
Before we create our volume let’s check the system for any existing volumes.
[sahil@linuxnix ~]$ docker volume ls DRIVER VOLUME NAME [sahil@linuxnix ~]$
We’ll now create out docker volume using the docker volume create command.
[sahil@linuxnix ~]$ docker volume create linuxnixvol linuxnixvol
Let’s run docker volume ls again to verify that our volume has been created.
[sahil@linuxnix ~]$ docker volume ls DRIVER VOLUME NAME local linuxnixvol [sahil@linuxnix ~]$
Step 2: Retrieve info about the volume
[sahil@linuxnix ~]$ docker volume inspect linuxnixvol [ { "CreatedAt": "2019-07-10T08:10:00Z", "Driver": "local", "Labels": {}, "Mountpoint": "/var/lib/docker/volumes/linuxnixvol/_data", "Name": "linuxnixvol", "Options": {}, "Scope": "local" } ] [sahil@linuxnix ~]$
Step 3: Mount the volume
To mount the volume we’ll create a new docker container and use the –mount option to specify the volume that we would like to mount. At this juncture, we would also need to mention the directory/location in the container where we would like to mount this volume.
[sahil@linuxnix ~]$ docker container run -d --name mountest --mount source=linuxnixvol,target=/apps nginx 2228d819306299fe22af27a1f4fd0c9846febc3c3ebb81fe72294b345196035e [sahil@linuxnix ~]$
In the above example, we’ve mounted the linuxnixvol that we created on the /apps directory inside the container. If you run the docker container inspect mountest command and scroll to the Mounts section you will see the below information about the mounted volume.
"Mounts": [ { "Type": "volume", "Name": "linuxnixvol", "Source": "/var/lib/docker/volumes/linuxnixvol/_data", "Destination": "/apps", "Driver": "local", "Mode": "z", "RW": true, "Propagation": ""
Step 4: Add data to the volume and verify data persistence
Now that our volume has been mounted in the container let’s login to the container and take a look. To login to the container in an interactive mode, we’ll be using the docker exec command.
[sahil@linuxnix ~]$ docker exec -it 2228d8193062 /bin/bash root@2228d8193062:/# ls apps bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
We see that the apps directory exists on which our exported volume is mounted. Let’s created a file in this directory.
root@2228d8193062:/# cd apps/ root@2228d8193062:~# ls -l /apps/ total 4 -rw-r--r--. 1 root root 12 Jul 10 08:49 hello.txt root@2228d8193062:~# read escape sequence [sahil@linuxnix ~]$
We’ve created a file in /apps and logged out using the ctrl+p ctrl+q sequence so that the container keeps running. Now let’s check the volume location on the host to verify that our file exists.
[sahil@linuxnix ~]$ sudo ls -ltr /var/lib/docker/volumes/linuxnixvol/_data total 4 -rw-r--r--. 1 root root 12 Jul 10 08:49 hello.txt [sahil@linuxnix ~]$
Now if we remove the container and then check for the file again we will find it to be still present.
[sahil@linuxnix ~]$ docker container stop 2228d8193062 2228d8193062 [sahil@linuxnix ~]$ docker container rm 2228d8193062 2228d8193062 [sahil@linuxnix ~]$ sudo ls -ltr /var/lib/docker/volumes/linuxnixvol/_data total 4 -rw-r--r--. 1 root root 12 Jul 10 08:49 hello.txt [sahil@linuxnix ~]$
Conclusion
This concludes our quick demonstration of data persistence with docker volumes. We hope that you found this post to be useful and look forward to your suggestion and feedback.
Sahil Suri
Latest posts by Sahil Suri (see all)
- Google Cloud basics: Activate Cloud Shell - May 19, 2021
- Create persistent swap partition on Azure Linux VM - May 18, 2021
- DNF, YUM and RPM package manager comparison - May 17, 2021
- Introduction to the aptitude package manager for Ubuntu - March 26, 2021
- zypper package management tool examples for managing packages on SUSE Linux - March 26, 2021