Create two containers with Persistent Storage

Sonam Kumari Singh
3 min readFeb 27, 2023

--

Storage — : when you create or save any file, photo or software to your system/ drive, All the data store in your Hard disk driver.

Persistent storage in docker — :

Persistent storage means when we are stopping or terminating the container the data should be persistent(i.e. The files will not delete automatically when the container is deleted .)

How do you ensure that the data is not removed when the container is terminated ?

When you launch any operating system with the help of docker. So behind the scene Docker engine does lots of task such as provide storage/hard disk, network, complete new filesystem & CPU/RAM etc to the container.

So, if you want to see all the details used this command ( docker info <container-name> )

Docker gives the ephemeral storage , which means when container is terminated, your entire data is lost. you can’t restore.

So, To make our data permanent we need some persistent storage. So the solution is we mount/link some directory/drive from base OS(rhel) to the container. It will work like a pendrive. Whatever data we will store in it, data will be persistent.

Storage is also known as volume

Volumes are the best way to data persistent in Docker. mount/link may be stored anywhere on the host system.

Steps-1

Create a drive/directory in your baseOS (# mkdir /web )

Add some example content ( # touch a b or cat > index.html )

Now, Launch container by attaching this volume

# docker run -it -name <container-name> -v /web:/var/www/html <image-name>:<tag>

Now, you inside container & setup the enviroment

# yum install httpd -y

Start the webserver using below command (# httpd )

create new file there ( cat > index.html ) and put some content

Test the webserver ( curl http://localhost-ip:80 )

then, exit from the container.

Now, terminate the os1(container) . Also check baseOS drive.

# docker rm <container-name>

Step 2 (Launch 2nd container)

# cd /web or # ls ( you can see there data)

Now, if you want to launch another container with same data, then just mount the volume while running.

one can be the data inside web server. we put our web page in doc. root ( /var/www/html). So, when we containerize the web server just mount the document root with some other drive of baseOS.

# docker run -it — name os2 -v /web:/var/www/html centos:latest

Now, you inside container & setup the enviroment again

# yum install httpd -y

Start the webserver using below command (# httpd )

Test the webserver ( curl http://localhost-ip/index.html )

Here we achieved the persistent storage in containers

Hope you liked it 😊

--

--

Sonam Kumari Singh

SONAM here! Grateful for your connection! Tech enthusiast exploring new languages, deep into DevOps, with a spotlight on Linux. 😊🚀