Using Docker, you can use volumes to persist data even when you stop or restart containers. We will create and use Docker volumes for PostgreSQL.
Prerequisites
To follow this tutorial:
- You should have Docker installed on your machine
- You should be comfortable with Docker and PostgreSQL commands.
Step 1: Extract the PostgreSQL image
First, we pull the PostgreSQL image from DockerHub:
Step 2: Create a Docker volume
Next, we will create a Docker volume to store the data. This volume will retain the data even if the container is deleted.
$ docker volume create pg_data
Step 3: Run the PostgreSQL container
Now that we have extracted the image and created a volume, we can run the PostgreSQL container by attaching the created volume.
$ docker run -d \
--name my_postgres \
-e POSTGRES_PASSWORD=mysecretpassword \
-v pg_data:/var/lib/postgresql/data \
-p 5432:5432 \
postgres
This command is executed my_postgres
in standalone mode. Using –v pg_data:/var/lib/postgresql/data mount 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: Check volume usage
Now that we have created the volume, we can verify that it is being used. You can inspect the volume and check its contents.
$ docker volume inspect pgdata
Running this command will display details about the volume, including its mount point on the host system. You can navigate to this directory and view 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:
Connect to the new database:
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', '[email protected]'), ('Bob', '[email protected]');
Run a sample query:
Production:
id | name | email
----+------+------------------
1 | Abby | [email protected]
2 | Bob | [email protected]
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:
Check the data in the users
table:
You should still see the result:
id | name | email
----+------+------------------
1 | Abby | [email protected]
2 | Bob | [email protected]
I hope this tutorial helps you understand how to use volumes to persist data when working with Docker.
Additional Resources
For more information, please read the following resources:
Happy exploring!
twitter.com/balawc27″ rel=”noopener”>Bala Priya C. Bala is a technical developer and writer from India. She enjoys working at the intersection of mathematics, programming, data science, and content creation. Her areas of interest and expertise include DevOps, data science, and natural language processing. She enjoys reading, writing, programming, and drinking coffee! Currently, she is working on learning and sharing her knowledge with the developer community by creating tutorials, how-to guides, opinion pieces, and more. Bala also creates interesting resource overviews and coding tutorials.
<script async src="//platform.twitter.com/widgets.js” charset=”utf-8″>