CARVIEW |
How To Use Docker Volumes for Persistent Data Storage
Learn how to use Docker volumes to ensure data persistence when working with Docker.

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:
$ docker pull postgres
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.
$ docker volume create pg_data
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.
$ docker run -d \
--name my_postgres \
-e POSTGRES_PASSWORD=mysecretpassword \
-v pg_data:/var/lib/postgresql/data \
-p 5432:5432 \
postgres
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.
$ docker volume inspect pgdata
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.
[
{
"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 Table
Connect to the Postgres instance and create a database and table.
Start a psql session:
$ docker exec -it my_postgres psql -U postgres
Create a new database:
CREATE DATABASE mydb;
Connect to the new database:
\c mydb
Create a table and insert some data:
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');
Run a sample query:
SELECT * FROM users;
Output:
id | name | email
----+------+------------------
1 | Abby | abby@example.com
2 | Bob | bob@example.com
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.
$ docker stop my_postgres
$ docker rm my_postgres
Step 7: Re-run the Postgres Container with the Same Volume
Start a new PostgreSQL container with the same volume to ensure data persistence.
$ docker run -d \
--name my_postgres \
-e POSTGRES_PASSWORD=mysecretpassword \
-v pgdata:/var/lib/postgresql/data \
-p 5432:5432 \
postgres
Connect to the Postgres instance and verify that the data persists.
Open a psql session:
$ docker exec -it my_postgres psql -U postgres
Connect to the mydb
database:
\c mydb
Verify the data in the users
table:
SELECT * FROM users;
You should still see the output:
id | name | email
----+------+------------------
1 | Abby | abby@example.com
2 | Bob | bob@example.com
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.
- Optimizing Data Storage: Exploring Data Types and Normalization in SQL
- How to Build and Publish a Docker Image to Docker Hub
- 7 Essential Ready-To-Use Data Engineering Docker Containers
- How to Use Docker for Local Development Environments
- Don't Manage Your Python Environments, Just Use Docker Containers
- How To Use Docker Tags to Manage Image Versions Effectively
Latest Posts
- We Benchmarked DuckDB, SQLite, and Pandas on 1M Rows: Here’s What Happened
- Prompt Engineering Templates That Work: 7 Copy-Paste Recipes for LLMs
- A Complete Guide to Seaborn
- 10 Command-Line Tools Every Data Scientist Should Know
- How I Actually Use Statistics as a Data Scientist
- The Lazy Data Scientist’s Guide to Exploratory Data Analysis
Top Posts |
---|
- 5 Fun AI Agent Projects for Absolute Beginners
- How I Actually Use Statistics as a Data Scientist
- The Lazy Data Scientist’s Guide to Exploratory Data Analysis
- Prompt Engineering Templates That Work: 7 Copy-Paste Recipes for LLMs
- 10 Command-Line Tools Every Data Scientist Should Know
- A Gentle Introduction to TypeScript for Python Programmers
- We Benchmarked DuckDB, SQLite, and Pandas on 1M Rows: Here’s What Happened
- A Complete Guide to Seaborn
- From Excel to Python: 7 Steps Analysts Can Take Today
- A Gentle Introduction to MCP Servers and Clients