Docker containers are aimed at creating and managing application environments that can be deployed in the matter of a few seconds. Containers are usually created for temporary purposes, depending on its business application.
When a container’s application purpose is served, it can be deleted and the resources allotted to it can be reused for creating new containers for other purposes, especially when used in development and testing services.
Why do you need persistent Docker storage?
The default Docker storage is not persistent in nature, which means that the data in the container would be lost upon a crash or deletion of the container.
Many online businesses that use Docker infrastructure may require persistent data for their containers, such as in the case of an application that stores data in the database for future use.
To share data among the Docker containers and to ensure that the data is secure and safe even after a container rebuild, you need to configure persistent data storage in your system.
There are some workarounds for ensuring persistent docker storage, such as data volumes, data volume containers or plugins. Today, we’ll discuss how data volumes can be used with Docker containers to store data in a persistent manner.
[ You don’t have to lose your sleep to keep your customers happy. Our Hosting Support Specialists cover your servers and support your customers 24/7 at just $9.99/hour. ]
Using data volumes for persistent Docker storage
In Docker, persistent storage can be achieved with the help of data volumes. Data volume is a directory in the Docker machine, which can store container data.
Once the data volumes are created, they can be shared among containers. The data stored in these data volumes persist even after deleting the container, and so can be used for purposes such as coding.
The default data volume directory is ‘/var/lib/docker/volumes’. This directory would contain the volumes specific to each container and they are identified using volume IDs – 64-bit randomly generated numbers.
Create data volumes for docker storage
To use data volumes, the volume has to be created first. To create a data volume, use the command:
docker volume create <data-volume> -o size=100GB
This command will create a data-volume with size 100GB in the host machine.
Once the data volume is created, we can create containers with this data volume associated to them. The data volume name can be given as parameter while creating containers using the Dockerfile or with ‘-v’ flag in ‘docker run’ command:
docker run -d -P --name web -v /data-volume nginx
When a container is created, a data volume would be associated with it in the folder ‘/data-volume’. By specifying the data volume name during container creation, it is possible to share data among containers.
To ensure that data corruption does not occur due to simultaneous access to the data by multiple applications in different Docker containers, care has to be exercised in managing the application ‘writes’.
[ Use your time to build your business. We’ll take care of your customers. Hire Our Hosting Support Specialists at $9.99/hr. ]
Manage data volumes in Docker
The list of data volumes in a host machine can be found using the ‘docker volume ls’ command. For easily identifying the data volumes, we can assign names to these volumes.
The ‘docker volume’ listing command will not give the container ID for that volume. To know the container to which each volume is linked to, ‘docker inspect [container-name]‘ command has to be used for each container.
In the results that is obtained for the inspect command for a Docker container, check out the section ‘Mounts’ to know the location of the data volume for that container.
Whatever data is written into the data directory of that container (destination: /var/lib/registry) would be stored in the data volume (source: ‘/var/lib/docker/volumes/0468c4755f2a6a746dcb806953f3b756756c332f19dd09785be84776aea80018/’).
Points to note..
Today we saw how to create data volumes and associate container data to them. To safeguard the data in the Docker system, it is always recommended to take timely backups to an external server.
The benefit of data volumes is that they are not deleted even after container deletion. But this can lead to wastage of disk space in the long run and it is crucial to find a way to identify such orphaned data volumes and retrieve the wasted space.
There are other work-around for persistent storage, like storage plugins, data volume containers or directory mounts, etc. and the choice varies with resource availability and business requirements.
Also, in the case of shared data storage for containers, the application code should be customized in a way to regulate multiple applications writing to the same space and messing up the data.
If you’d like to know how to manage your Docker system and to get the best out of them for your business, we’d be happy to talk to you.
0 Comments