Â
When utilizing Docker, you should use volumes to persist information even whenever you cease or restart the containers. We’ll create and use Docker volumes for PostgreSQL.
Conditions
Â
To observe together with this tutorial:
- It is best 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 will 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/information
-p 5432:5432
postgres
Â
This command runs my_postgres
in indifferent mode. Utilizing –v pg_data:/var/lib/postgresql/information mounts the pg_data
quantity to /var/lib/postgresql/information 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 quantity, we will confirm it’s getting used. You possibly can examine the quantity and test 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 possibly can navigate to this listing and see the PostgreSQL information 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 information:
CREATE TABLE customers (
id SERIAL PRIMARY KEY,
title VARCHAR(100),
e mail VARCHAR(100)
);
INSERT INTO customers (title, e mail) VALUES ('Abby', 'abby@instance.com'), ('Bob', 'bob@instance.com');
Â
Run a pattern question:
Â
Output:
id | title | e mail
----+------+------------------
1 | Abby | abby@instance.com
2 | Bob | bob@instance.com
Â
Step 6: Cease and Take away the Container
Â
Cease the working container and take away it. We do that so we will take a look at 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 information persistence.
$ docker run -d
--name my_postgres
-e POSTGRES_PASSWORD=mysecretpassword
-v pgdata:/var/lib/postgresql/information
-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:
Â
It is best to nonetheless see the output:
id | title | e mail
----+------+------------------
1 | Abby | abby@instance.com
2 | Bob | bob@instance.com
Â
I hope this tutorial helps you perceive the right way to use volumes to persists information when working with Docker.
Â
Extra Sources
Â
To study extra, learn the next sources:
Comfortable exploring!
Â
Â
Bala Priya C is a developer and technical author from India. She likes working on the intersection of math, programming, information science, and content material creation. Her areas of curiosity and experience embody DevOps, information science, and pure language processing. She enjoys studying, writing, coding, and low! At the moment, she’s engaged on studying and sharing her information with the developer neighborhood by authoring tutorials, how-to guides, opinion items, and extra. Bala additionally creates participating useful resource overviews and coding tutorials.