Open In App

How to Use Docker For Stateful Applications with Persistent Volumes?

Last Updated : 22 Jul, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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?

  • Start the docker environment.

sudo systemctl start docker

  • Check for images which is to be containerize.

docker pull *image_name*

For Example:

Screenshot-from-2023-05-19-12-47-26.png

Example for Pulling an Image

docker images

  • Create a volume.

docker volume create *volume_name*

Screenshot-from-2023-05-19-13-00-20.png

Example for creating a volume

  • Run a Docker container with the volume attached:

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.

Screenshot-from-2023-05-19-13-05-34.png

Example for using volume in a container

  • Data will remain accessible even if the container is terminated or removed thanks to Docker’s management of the volume’s persistence.

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

Screenshot-from-2023-05-19-13-12-36.png

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

)

Screenshot-from-2023-05-19-13-16-15.png

Created file in the container pycontainer

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

Screenshot-from-2023-05-19-13-37-57.png

Terminated pycontainer

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

Screenshot-from-2023-05-19-13-40-37.png

my_volume in swiftcontainer

Accessing the volume on the local system:

Screenshot-from-2023-05-19-13-55-34.png

Inspecting Volume

Screenshot-from-2023-05-19-13-58-08.png

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

  • When the container has to be updated or changed, you can halt the process and delete the old container:

docker stop *container_name*

docker rm *container_name*

  • Remove or Delete the volume

docker volume rm *volume_name*

Screenshot-from-2023-05-19-13-45-43.png

Removing Volume

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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads