reCAPTCHA WAF Session Token
Data Science and ML

How To Use Docker Volumes for Persistent Data Storage

Thank you for reading this post, don't forget to subscribe!


 

When using Docker, you can use volumes to persist data even when you stop or restart the containers. We’ll create and use Docker volumes for PostgreSQL.

Prerequisites

 

To follow along with this tutorial:

  • You should have Docker installed on your machine
  • You should be comfortable with Docker commands and PostgreSQL

 

Step 1: Pull the PostgreSQL Image

 

First, we pull the PostgreSQL image from DockerHub:

 

Step 2: Create a Docker Volume

 

Next, let’s create a Docker volume to store the data. This volume will persist the data even if the container is removed.

<code>$ docker volume create pg_data</code>

 

Step 3: Run the PostgreSQL Container

 

Now that we’ve pulled the image and created a volume, we can run the PostgreSQL container attaching the created volume to it.

<code>$ docker run -d \
	--name my_postgres \
	-e POSTGRES_PASSWORD=mysecretpassword \
	-v pg_data:/var/lib/postgresql/data \
	-p 5432:5432 \
	postgres</code>

 

This command runs my_postgres in detached mode. Using –v pg_data:/var/lib/postgresql/data mounts the pg_data volume to /var/lib/postgresql/data in the container. And using -p 5432:5432 maps port 5432 of my_postgres to port 5432 on the host machine.

 

Step 4: Verify the Volume Usage

 

Now that we’ve created the volume, we can verify it’s being used. You can inspect the volume and check the contents.

<code>$ docker volume inspect pgdata</code>

 

Running this command will show details about the volume, including its mount point on your host system. You can navigate to this directory and see the PostgreSQL data files.

<code>[
	
    	"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"
	
]</code>

 

Step 5: Create a Database and Table

 

Connect to the Postgres instance and create a database and table.

Start a psql session:

<code>$ docker exec -it my_postgres psql -U postgres</code>

 

Create a new database:

 

Connect to the new database:

 

Create a table and insert some data:

<code>CREATE TABLE users (
    id SERIAL PRIMARY KEY,
    name VARCHAR(100),
    email VARCHAR(100)
);

INSERT INTO users (name, email) VALUES ('Abby', 'abby@example.com'), ('Bob', 'bob@example.com');</code>

 

Run a sample query:

 

Output:

<code> id | name |  	email  	 
----+------+------------------
  1 | Abby | abby@example.com
  2 | Bob  | bob@example.com</code>

 

Step 6: Stop and Remove the Container

 

Stop the running container and remove it. We do this so we can test that the data persists even if the container is stopped.

<code>$ docker stop my_postgres
$ docker rm my_postgres</code>

 

 

Step 7: Re-run the Postgres Container with the Same Volume

 

Start a new PostgreSQL container with the same volume to ensure data persistence.

<code>$ docker run -d \
	--name my_postgres \
	-e POSTGRES_PASSWORD=mysecretpassword \
	-v pgdata:/var/lib/postgresql/data \
	-p 5432:5432 \
	postgres</code>

 

Connect to the Postgres instance and verify that the data persists.

Open a psql session:

<code>$ docker exec -it my_postgres psql -U postgres</code>

 

Connect to the mydb database:

 

Verify the data in the users table:

 

You should still see the output:

<code> id | name |  	email  	 
----+------+------------------
  1 | Abby | abby@example.com
  2 | Bob  | bob@example.com</code>

 

I hope this tutorial helps you understand how to use volumes to persists data when working with Docker.

 

Additional Resources

 

To learn more, read the following resources:

Happy exploring!

 
 

Bala Priya C is a developer and technical writer from India. She likes working at the intersection of math, programming, data science, and content creation. Her areas of interest and expertise include DevOps, data science, and natural language processing. She enjoys reading, writing, coding, and coffee! Currently, she’s working on learning and sharing her knowledge with the developer community by authoring tutorials, how-to guides, opinion pieces, and more. Bala also creates engaging resource overviews and coding tutorials.



Back to top button
Consent Preferences
WP Twitter Auto Publish Powered By : XYZScripts.com
SiteLock