How to persist data in a dockerized postgres database using volumes
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Persisting Data in a Dockerized PostgreSQL Database Using Volumes
Docker's design principle is to enable stateless and ephemeral containers. When a container is removed, all of its data is destroyed. In scenarios where data persistence is crucial, such as for databases, Docker volumes provide a reliable method. This article explores the ins and outs of using Docker volumes to persist data with a PostgreSQL database.
Concept of Docker Volumes
Docker volumes are a preferred mechanism for persisting data generated by and used by Docker containers. Volumes are stored outside of the container’s writable layer, avoiding that layer's performance and storage constraints. They enable data sharing among multiple containers and outlive the lifecycle of individual containers.
Setting Up PostgreSQL with Docker Volumes
Below, we outline the steps to deploy a PostgreSQL Docker container using a volume to persist its data.
- Install Docker: Ensure you have Docker installed on your system. You can verify installation by running
docker --version. - Pull the PostgreSQL Image: To start, pull the official PostgreSQL image from Docker Hub.
- Create a Docker Volume: Create a named volume that PostgreSQL can use to store its data.
- Run a PostgreSQL Container: Launch a PostgreSQL container with the created volume. Here’s an example command:
--name my-postgresgives the container a friendly name.-e POSTGRES_PASSWORD=mysecretpasswordsets the PostgreSQLrootaccount password.-v pgdata:/var/lib/postgresql/datamounts thepgdatavolume to the PostgreSQL data directory.-d postgresruns the container detached, using thepostgresimage.
- Interact with PostgreSQL: To enter the running PostgreSQL container and interact with the database, use:
You'll be dropped into the PostgreSQL interactive terminal, where you can perform database operations.
Persisting Data Across Container Restarts
To demonstrate data persistence, you can insert data, stop or remove the running container, and start it again:
Stop and remove the container:
Run the container again:
Verify data persists:
Advantages and Disadvantages
Here's a summary of using Docker volumes for data persistence:
| Advantages | Disadvantages |
| Data survives container restarts | Volumes are Docker-specific, less portable on non-Docker systems |
| Allows sharing data between containers | Requires learning curve for volume management |
| Optimized performance for storage | Mismanagement can lead to dangling volumes |
Additional Details
- Creating and Pruning Volumes: Use
docker volume lsto list volumes anddocker volume rm VOLUME_NAMEto remove unused volumes.docker volume prunecan clean up all unused volumes. - Data Volume Containers: Instead of directly mounting to host paths, you can use data volume containers that hold the data volume and mount it to other containers.
- Backups: Regular database backups are essential. While volumes protect data from container removal, they won’t shield it from corruption or host-level failures.
- Security Concerns: Be cautious with environment variables for sensitive data. Consider using Docker Secrets for production environments.
Conclusion
Combining Docker with PostgreSQL via volumes is an effective way to maintain data persistency across container lifecycles. By leveraging Docker volumes, developers can ensure their applications remain both flexible and resilient, augmenting the benefits of containerized environments with persistent storage solutions.
Related reading
- How to persist data using a postgres database, Docker, and Kubernetes?
- How to prevent docker from starting a container automatically on system startup?
- How to publish ports in docker files?
- How to pull a private container from AWS ECR to a local cluster
- How to pg_dump an RDS Postgres database?
- How to pick a Kafka transaction.id
- How to pull docker images hosted on Google Container Registry via Kubernetes kubernetes included on docker for desktop
- How to pull image from dockerhub in kubernetes?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.