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.
We’ll now create out docker volume using the docker volume create command.
[[email protected] ~]$ docker volume create linuxnixvol
Let’s run docker volume ls again to verify that our volume has been created.
Step 2: Retrieve info about the volume
[[email protected] ~]$ docker volume inspect linuxnixvol
[[email protected] ~]$
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.
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.
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.
We see that the apps directory exists on which our exported volume is mounted. Let’s created a file in this directory.
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.
Now if we remove the container and then check for the file again we will find it to be still present.
[[email protected] ~]$ docker container stop 2228d8193062
[[email protected] ~]$ docker container rm 2228d8193062
[[email protected] ~]$ sudo ls -ltr /var/lib/docker/volumes/linuxnixvol/_data
-rw-r–r–. 1 root root 12 Jul 10 08:49 hello.txt
[[email protected] ~]$
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.
The following two tabs change content below.
He started his career in IT in 2011 as a system administrator. He has since worked with HP-UX, Solaris and Linux operating systems along with exposure to high availability and virtualization solutions.
He has a keen interest in shell, Python and Perl scripting and is learning the ropes on AWS cloud, DevOps tools, and methodologies. He enjoys sharing the knowledge he’s gained over the years with the rest of the community.
dockerfile volume example