How To Use Docker Volumes for Persistent Information Storage

Date:

Share post:



 

When utilizing Docker, you need to use volumes to persist knowledge even once you cease or restart the containers. We’ll create and use Docker volumes for PostgreSQL.

Stipulations

 

To observe together with this tutorial:

  • You need to have Docker put in in your machine
  • You have to be snug with Docker instructions and PostgreSQL

 

Step 1: Pull the PostgreSQL Picture

 

First, we pull the PostgreSQL picture from DockerHub:

 

Step 2: Create a Docker Quantity

 

Subsequent, let’s create a Docker quantity to retailer the information. This quantity will persist the information even when the container is eliminated.

$ docker quantity create pg_data

 

Step 3: Run the PostgreSQL Container

 

Now that we’ve pulled the picture and created a quantity, we are able to run the PostgreSQL container attaching the created quantity to it.

$ docker run -d 
	--name my_postgres 
	-e POSTGRES_PASSWORD=mysecretpassword 
	-v pg_data:/var/lib/postgresql/knowledge 
	-p 5432:5432 
	postgres

 

This command runs my_postgres in indifferent mode. Utilizing –v pg_data:/var/lib/postgresql/knowledge mounts the pg_data quantity to /var/lib/postgresql/knowledge within the container. And utilizing -p 5432:5432 maps port 5432 of my_postgres to port 5432 on the host machine.

 

Step 4: Confirm the Quantity Utilization

 

Now that we’ve created the amount, we are able to confirm it’s getting used. You may examine the amount and verify the contents.

$ docker quantity examine pgdata

 

Operating this command will present particulars in regards to the quantity, together with its mount level in your host system. You may navigate to this listing and see the PostgreSQL knowledge recordsdata.

[
	{
    	"CreatedAt": "2024-08-07T15:53:23+05:30",
    	"Driver": "local",
    	"Labels": null,
    	"Mountpoint": "/var/lib/docker/volumes/pg_data/_data",
    	"Name": "pg_data",
    	"Options": null,
    	"Scope": "local"
	}
]

 

Step 5: Create a Database and Desk

 

Hook up with the Postgres occasion and create a database and desk.

Begin a psql session:

$ docker exec -it my_postgres psql -U postgres

 

Create a brand new database:

 

Hook up with the brand new database:

 

Create a desk and insert some knowledge:

CREATE TABLE customers (
    id SERIAL PRIMARY KEY,
    identify VARCHAR(100),
    electronic mail VARCHAR(100)
);

INSERT INTO customers (identify, electronic mail) VALUES ('Abby', 'abby@instance.com'), ('Bob', 'bob@instance.com');

 

Run a pattern question:

 

Output:

 id | identify |  	electronic mail  	 
----+------+------------------
  1 | Abby | abby@instance.com
  2 | Bob  | bob@instance.com

 

Step 6: Cease and Take away the Container

 

Cease the operating container and take away it. We do that so we are able to check that the information persists even when the container is stopped.

$ docker cease my_postgres
$ docker rm my_postgres

 

 

Step 7: Re-run the Postgres Container with the Identical Quantity

 

Begin a brand new PostgreSQL container with the identical quantity to make sure knowledge persistence.

$ docker run -d 
	--name my_postgres 
	-e POSTGRES_PASSWORD=mysecretpassword 
	-v pgdata:/var/lib/postgresql/knowledge 
	-p 5432:5432 
	postgres

 

Hook up with the Postgres occasion and confirm that the information persists.

Open a psql session:

$ docker exec -it my_postgres psql -U postgres

 

Hook up with the mydb database:

 

Confirm the information within the customers desk:

 

You need to nonetheless see the output:

 id | identify |  	electronic mail  	 
----+------+------------------
  1 | Abby | abby@instance.com
  2 | Bob  | bob@instance.com

 

I hope this tutorial helps you perceive find out how to use volumes to persists knowledge when working with Docker.

 

Further Sources

 

To be taught extra, learn the next assets:

Completely satisfied exploring!

 
 

Bala Priya C is a developer and technical author from India. She likes working on the intersection of math, programming, knowledge science, and content material creation. Her areas of curiosity and experience embody DevOps, knowledge science, and pure language processing. She enjoys studying, writing, coding, and occasional! At the moment, she’s engaged on studying and sharing her information with the developer group by authoring tutorials, how-to guides, opinion items, and extra. Bala additionally creates participating useful resource overviews and coding tutorials.

Related articles

The right way to Use R for Textual content Mining

Picture by Editor | Ideogram   Textual content mining helps us get essential info from massive quantities of textual content....

Final Roadmap to Changing into a Tech Skilled with Harvard for Free

Picture by Creator | Canva   For those who’re a part of the KDnuggets group, it means you’re already a...

10 Finest Worker Engagement Software program Platforms (October 2024)

A lot of right now's worker engagement platforms are leveraging synthetic intelligence to enhance how organizations join with,...

Environment friendly Data Administration for Knowledge Groups Utilizing Notion

Picture by Editor | Ideogram   A corporation's information groups typically encounter complicated tasks with a wide range of assets...