Updated April 4, 2023
Introduction to Persistent Storage Docker
In normal words, persistent storage is nothing but the data storage platform or the device. There are two major types of data storage the volatile storage and non-volatile storage. In docker or the containerization platform, persistent storage is used to keep the volumes. Normally, it associates with the stateful level application. In the stateful application, you can consider the database, etc. In the container, the data will be available still the current life of the container. The persistent storage volumes are opposite to the volumes of the ephemeral storage. The state (like they live or die) of the persistent storage volumes will be associated with the stateless application.
docker run -d --name=< image name > --mount source=< source path >, destination= < destination path > < docker image name >
1) docker: We can use the docker keyword in the syntax or command. It will accept different arguments like run, image path, storage type, etc. As per the provided input, the docker command will be able to do the necessary action and launch the container.
Note: while lunching the container, the docker will launch the container in the background as well as the foreground. As per the requirement, we need to provide the input to the docker command.
2) OPTION: There are multiple options available in the docker environment liked (it will use to run the docker image in the background), –name (we can provide the own image name to the docker image), destination (we can define the destination path), etc. As per the requirement, we can use the different flags as the option that is compatible with the docker command.
3) –mount source: As per the requirement, we can use the different storage options to the docker command. It will automatically align with the docker container.
How Persistent storage Docker Works?
In docker, the persistent storage is dealt with the volume concept. Persistent storage is means when we are stopping or removing the container the data should be persistent. It will not delete automatically once the docker container is not available. We can define the persistent storage layer while launching the docker container. Once it will define, the actual data will be store on the external storage layer which is not a part of the docker environment. The storage layer will be on the host machine, remote machine, on the cloud, etc. Before directly going to the docker persistent storage, we need to first understand the storage layer availability on the docker platform. It will help to understand the storage layer concept.
We are having the list of docker images. The docker images are available on the docker hub. If we need to create our own images then we can also build a new or own image. Please note, while creating the new image, we need to choose the respective base image like centos, Ubuntu, windows, etc. Once the image is ready, we can run the container in the docker. If we are normally running the container without any storage definition then the docker data is in a volatile state. In the docker, there are three different ways that the data will be store.
1. bind mounts
3. in-memory storage (tmpfs)
Bind mounts: The bind mounts are a very older method to mount the file or the directory to the docker container. The bind mount is having limited functionality as compared to the volume. When we are using the bind mount method, we are using the local file or the local directory to the docker container. We need to define the complete path of the local file or the local directory of the local machine into the docker container. While working with the bind mounts, we need to rely on the local machine file structure. We need to follow the set of rules and regulations which is imposed on the local system.
Volumes: The volume storage is also known as the persistent data storage layer of the docker. The default path for the docker volume is /var/lib/docker/volumes. Whenever we are creating or associating the volume to the docker container. The volume will by default store on the “/var/lib/docker/volumes” path. Volume is very easier to mount or for the migration. If we are deleting the docker container but the volume is still present on the storage layer. Then the same volume we can use for the next or the different docker container. It will use the same data which was previously used by the older container.
In-memory storage (temporary storage file): When we are launching the docker container without defining the storage layer like mount or volume. The docker container will run under the in-memory storage file system. It will store the data temporarily. Once we are deleting or stopping the respective docker container the data will vanish. The working data is not present in it. We need to rerun or load the data one more time if we need to work on the same docker container.
In the docker environment, we are able to add persistent storage to the docker image.
docker run -d --name=own_img --mount source=nginx-vol,destination=/usr/share/nginx/html nginx
As per the above command, we are adding persistent storage to the Nginx docker image. Here, we are using the container name as the “own_img”.
We have seen the uncut concept of the “Persistent storage Docker” with the proper example, explanation, and command with different outputs. When we are defining the persistent storage in the docker then the data is available on the Docker host machine or the remote Docker machine. The will be present if we are deleting the docker container also. The docker persistent storage is more important for the developer front.
This is a guide to Persistent Storage Docker. Here we discuss the Introduction, syntax, How Persistent storage Docker Works? examples with code implementation. You may also have a look at the following articles to learn more –