Volume & Persistence Data:
There are two types of Data, Persistence Data which is needed to be kept and is used in future, where as Non- Persistence data is one what in not required to be kept for long days.
Every Docker container has non-persistence storage, this type of storage is automatically created, and is tied to lifecycle of container. That means that if container is deleted, its tied storage is also deleted.
In this non-persistence storage all the container files and file systems are stored.
On Linux system, storage is exists under /var/lib/docker/<storage-driver>/ as part of container.
Following are Linux drivers mentioned here
- Red Hat Enterprise Linux: overlay 2 driver
- Ubuntu: overlay2 or aufs driver
- SUSE Linux Enterprise server: Btrfs storage driver
- Windows: It has only driver that is configured by default.
Now if you need to keep data safe even after container is deleted, you need to create volume. These Volume are created and managed separately. Even you delete container, Volume is not automatically created, unless it is explicitly deleted.
Containers and Persistence Data
In this, you should create Volume and the create containers and then you should mount volume to your containers.
The volume gets mounted to a directory in container file systems, and anything written to that directory gets written to volume and even after that container is deleted, the volume remains on docker host and your data is still safe.
Docker volume is mounted in to a container at /code. Any data written to /code directory will be stored on volume and will exits after container is deleted.
Create Docker Volume
Now let’s create Docker Volume, Commands to be run on docker host is given below figure.
Here we are creating docker volume whose name is dclessons-vol and once it is created, it can be viewed by docker volume ls command and you can even inspect this volume to see that on which directory it is mapped to.
In the above example it is attached to /var/lib/docker/volumes/dclessons-vol/_data
Now in order to delete the Volume there are two commands:
- Docker volume prune
- Docker volume rm
The above command will delete all volume in the docker host whereas second will only delete specific volume which you will ask for.
Create Volume and attach it to Containers:
In below figure we are creating volume name dclessons-vol and attaching to container named devtest which runs ngnix service.
Now let’s see how to write any file on volume and how to see the output. Here in below figure we are creating a container from Ubuntu image by using – -rm, so that when we exit from container, it will delete it, we’ll also use -v to mount the new volume. -v requires the name of the volume, a colon, and then the absolute path to where the volume should appear inside the container.
Because we used the –rm flag, our container will be automatically deleted when we exit. Our volume, however, will still be accessible.
Now let’s write some text in file dclessons.txt on that volume.
We can verify that the volume is present on our system with docker volume inspect: docker volume inspect dclessons-vol
Now if you want to see the content of file, you should cat command
Creating Volume at time of Container creation:
Let’s see, how we can create a volume at same time container is created, to do this use following command:
Use the docker run command to create a new container using the base Ubuntu image. -t will give us a terminal, and -i will allow us to interact with it. For clarity, we’ll use –name to identify the container.
To write some text on volume created, use following commands
Now when you start container, the volume will be attached automatically.
Now remove the Volume created, but it will show to which container it is attached to , so we need to first delete the container and then that specific volume.