Open In App

How to Use Docker For Stateful Applications with Persistent Volumes?

Data persistence is provided through a controlled directory called a Docker Volume, which may be mounted inside Docker containers. When containers are stopped or deleted, it enables data to continue to exist. Volumes can be shared across containers and are unaffected by container lifecycles. They make data management and integrity maintenance in Dockerized environments simpler by giving stateful apps a dependable means to store and retrieve data.

Persistent volumes (PVs), a component of container orchestration platforms like Kubernetes, offer programmes reliable and independent storage. Data persists even after pods or containers are terminated because of PVs’ separation of storage from the application lifecycle. Different types of storage resources may be supplied and managed thanks to their features, which include replication, access modes, and storage classes. Data persistence and availability across container instances and deployments are made possible by the use of persistent volumes, which provide applications access to dependable, long-term storage that is independent of the underlying infrastructure.



How To Create and Use Volumes?

sudo systemctl start docker

docker pull *image_name*



For Example:

Example for Pulling an Image

docker images

docker volume create *volume_name*

Example for creating a volume

docker run -d -v *volume_name*:/path/in/container –name *container_name* *image_name*

Replace /path/in/container with the path within the container where you want to store the data.

Example for using volume in a container

For Example:- Create a file to be stored in the volume inside the running container.

Creating a file inside the container in the associated volume i.e. my_volume

Check whether file is created in the container i.e.

(

pycontainer

holding

my_volume

)

Created file in the container pycontainer

On terminating the pycontainer it should not affect the volume and it’s storage

Terminated pycontainer

Running and accessing the volume i.e., my_volume in another container i.e., swiftcontainer with a new image i.e, swift

my_volume in swiftcontainer

Accessing the volume on the local system:

Inspecting Volume

On heading to the Mountpoint address we can access the data in the volume, irrespective of the containers

docker stop *container_name*

docker rm *container_name*

docker volume rm *volume_name*

Removing Volume

Now, you can easily access and run the stateful applications on to the volumes and containerize them.


Article Tags :