Thursday, January 21, 2016

Docker – Data Volumes

In the previous articles we have seen how we can create containers from various images and created our own basic image for our use. But a container is just a image that we use and if we remove a container everything is gone. What if we want to write data from inside a container? Consider a application running inside a container want to store data. Now even if the container is removed and if in future we launch another container, we would like the data to be available there.

In other words, we are trying to get over here is to separate out the container lifecycle from the data. we want to keep these separate so that the data generated is not destroyed or tied to the container lifecycle and can thus be reused.

There are 2 ways in which we can manage data in the Docker and they are,
Data volumes
Data volume containers

A Data Volume is a area specially designed directory for the container. This is not handled with the Container life cycle but will be initialized during the container creation. The deletion is not done when the container is removed. No Garbage collection of this location happens.
We can use this data location when we create another container by sharing in read only mode (by default)

In this article we will see how we can create a Data Volume and use that

1) Create a Data Volume
[root@vx111a SampleTest]# docker run -it -v /data --name container1 busybox

We will be passing the “-v /data” parameter to the command line. Once the container is up and running, login and see data locations as

/ # ls
bin   data  dev   etc   home  proc  root  run   sys   tmp   usr   var

/ # cd data/
/data # touch hello.txt

/data # ls
hello.txt

/data # exit

We created a Text file with the name “hello.txt”. Exit the container after the file creation. Now check the “docker ps –a” command to see the status of the containers running as
  
[root@vx111a SampleTest]# docker ps -a
CONTAINER ID    IMAGE     COMMAND     CREATED     STATUS     PORTS    NAMES
92ae3eb0281a    busybox   "sh"              minute ago   Exited                    container1

We can see the busybox container that we started is in the Exited state.

2) Inspect the Container - Now lets inspect the Container using the inspect command

[root@vx111a SampleTest]# docker inspect container1

Once we run the command, we can see multiple lines of output of which the data volumes are of our interest, we can see a JSON output and we look for Volumes attribute in the output.

"Volumes": {
            "/data": {}
        },

 "Mounts": [
        {
            "Name": "3cda62a8f710cef37d7ad11843a19186470afa2e1c29a5b082bf969070b14118",
            "Source": "/var/lib/docker/volumes/3cda62a8f710cef37d7ad11843a19186470afa2e1c29a5b082bf969070b14118/_data",
            "Destination": "/data",
            "Driver": "local",
            "Mode": "",
            "RW": true
        }
    ],

We can see both Volumes and Mounts in the output. The Volumes tell us about the volumes mounted “/data” and Mounts tell us about the location where volumes are mounted. In this case the “/data” is mounted on the location “/var/lib/docker/volumes/3cda62a8f710cef37d7ad11843a19186470afa2e1c29a5b082bf969070b14118/_data”

Now we can go to the location and see the file that we have created inside the docker container,

[root@vx111a SampleTest]# cd /var/lib/docker/volumes/3cda62a8f710cef37d7ad11843a19186470afa2e1c29a5b082bf969070b14118/_data

[root@vx111a _data]# ll
total 0
-rw-r--r-- 1 root root 0 Jan 13 09:26 hello.txt

We can also see that the Volumes RW mode is set to true i.e. Read and Write.

3) Restart the Container – Now lets start the container again which we stopped previously.

[root@vx111a _data]# docker restart container1
container1

When we attach back to the Contianer1 we can see the data available
[root@vx111a _data]# docker attach container1

/ # ls
bin   data  dev   etc   home  proc  root  run   sys   tmp   usr   var
/ # cd data/
/data # ls
hello.txt

4) Remote the Container Completely – We can now stop the container and remove the Container using the “docker rm <container ID>”. Once the container is removed we can still see the data available

[root@vx111a _data]# cd /var/lib/docker/volumes/3cda62a8f710cef37d7ad11843a19186470afa2e1c29a5b082bf969070b14118/_data
[root@vx111a _data]# ll
total 0
-rw-r--r-- 1 root root 0 Jan 13 09:26 hello.txt

More to Come , Happy learning J

No comments :

Post a Comment