![]() |
Docker and Postgres are two widely used tools in the software development field. Docker simplifies the deployment process by encapsulating applications within a container while Postgres provides a robust and reliable database to store and manage data. In this guide, I will first briefly discuss Docker and Postgres. Then I will guide you through the various steps to persist your Postgres container data using docker volumes. What is DockerDocker encapsulates the application and its dependencies into compact units called containers. Containers contain everything that an application needs to run such as libraries, system tools, code, and runtime. This approach greatly enhances portability and scalability. It removes the dependencies of building, testing, and running an application on a particular operating system and hardware. Docker is a very fast, lightweight, and resource-efficient tool. Unlike the traditional virtualization techniques, docker containers share the host operating system kernel which helps the developers to run more containers on a single host. This results in maximizing resource utilization and reducing infrastructure costs. Overall, we can say that Docker has become a very important tool for developers and organizations to accelerate their software deployment and delivery pipelines. What is PostgresPostgreSQL is commonly referred to as Postgres. Postgres is an open-source relational database that is used for storing and managing data efficiently. It ensures data integrity as it follows the ACID properties. Postgres provides a variety of features including transactions, complex queries, indexing, and replication. In addition to its core features, Postgres also supports internationalization and text search. These features make Postgres a suitable choice for diverse linguistic and text processing needs. Postgres is widely used in many industries such as healthcare, finance, and telecommunications. We can say overall Postgres is a comprehensive and versatile solution for storing, managing, and analyzing data. Pre-requisitesBefore moving to the next section make sure you have installed Docker on your system. If you have not installed follow these detailed horje articles to install docker on your system.
Steps to Persist Data In A Dockerized Postgres Using VolumesStep 1: Create a docker volume. This volume will help in persisting the data. docker volume create postgres_volume
Step 2: Run a Postgres docker container using docker volume. docker run -d \ Step 3: Go inside the docker container. docker exec -it postgres psql -U postgres
Step 4: Create a database. CREATE DATABASE demo_db;
Step 5: Try to connect the database. \c demo_db
Step 6: Create a table inside the database and insert some demo data . Then exit. CREATE TABLE gfg_articles (id SERIAL PRIMARY KEY, name VARCHAR(255));
INSERT INTO gfg_articles (name) VALUES ('Docker 1'), ('Jenkins 2'), ('K8s 3');
You can use the below command to exit the Postgres terminal. \q
Step 7: Now delete the Postgres docker container. docker stop postgres Step 8: Create again a Postgres docker container using the docker volume (created in Step 1). docker run -d \ Step 9: Finally go inside the docker container and verify that whether the database and table is present or not. (run the commands below one by one ) docker exec -it postgres psql -U postgres ConclusionHere in this guide we first learn what is Docker . Then learned some basics about Postgres . Then we have followed various steps to persist the Postgres data using docker volumes . We started by creating a Postgres docker container using a docker volume . Then added some dummy database and dummy table to the database . Then deleted the Postgres docker container and recreate the Postgres docker container to verify whether the Postgres data persists or not . How to persist data in a dockerized postgres database using volumes-FAQ’sWhat is docker volume ?
What happens if i delete the Docker volume containing Postgres data ?
Why persisting Postgres data is required ?
Are their any limitation using docker volumes ?
How to secure docker volumes ?
|
Reffered: https://www.geeksforgeeks.org
Dev Scripter |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 12 |