Connect with us

Hi, what are you looking for?

Hosting

Explained by Docker Volumes-Linux Juggernaut

Explained by Docker Volumes-Linux Juggernaut

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.

[[email protected]uxnix ~]$ docker volume ls
DRIVER VOLUME NAME
[[email protected] ~]$

We’ll now create out docker volume using the docker volume create command.

[[email protected] ~]$ docker volume create linuxnixvol
linuxnixvol

Let’s run docker volume ls again to verify that our volume has been created.

[[email protected] ~]$ docker volume ls
DRIVER VOLUME NAME
local linuxnixvol
[[email protected] ~]$

Step 2: Retrieve info about the volume

[[email protected] ~]$ 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”
}
]
[[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.

Explained by Docker Volumes-Linux Juggernaut

[[email protected] ~]$ docker container run -d –name mountest –mount source=linuxnixvol,target=/apps nginx
2228d819306299fe22af27a1f4fd0c9846febc3c3ebb81fe72294b345196035e
[[email protected] ~]$

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.

[[email protected] ~]$ docker exec -it 2228d8193062 /bin/bash
[email protected]:/# 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.

[email protected]:/# cd apps/
[email protected]:~# ls -l /apps/
total 4
-rw-r–r–. 1 root root 12 Jul 10 08:49 hello.txt
[email protected]:~# read escape sequence
[[email protected] ~]$

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.

[[email protected] ~]$ sudo ls -ltr /var/lib/docker/volumes/linuxnixvol/_data
total 4
-rw-r–r–. 1 root root 12 Jul 10 08:49 hello.txt
[[email protected] ~]$

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
2228d8193062
[[email protected] ~]$ docker container rm 2228d8193062
2228d8193062
[[email protected] ~]$ sudo ls -ltr /var/lib/docker/volumes/linuxnixvol/_data
total 4
-rw-r–r–. 1 root root 12 Jul 10 08:49 hello.txt
[[email protected] ~]$

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.

Post Views:
8,918

The following two tabs change content below.

Explained by Docker Volumes-Linux Juggernaut

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.

Explained by Docker Volumes-Linux Juggernautdockerfile volume example

You May Also Like

Hosting

Introduction In previous articles we have talked about images of dockers, the origin and functioning of dockers and the dockers’ hub. In this document...

Latest

Linux desktops are good in many ways, but like Windows they are not known as the most efficient battery. This does not mean that...

Latest

The United States Supreme Court has indicated that it will finally solve a problem that has been causing legal problems for almost two decades:...

Hosting

To secure your AWS assets, follow these AWS Identity and Access Management (IAM) guidelines. Locking the Root User Access Key for AWSAccount You use...