How to Mount Docker Volume to PostgreSQL Container?

How to Mount Docker Volume to PostgreSQL Container

Docker is an open-source containerization platform that allows developers to create and run applications in a portable and efficient manner. PostgreSQL, on the other hand, is an open-source relational database management system. In this article, we will discuss how to mount a Docker volume to a PostgreSQL container.

What is a Docker Volume?

A Docker volume is a persistent data storage mechanism that can be used to share data between containers or between a container and the host machine. When a container is deleted, the data stored in a Docker volume is not lost.

Mounting a Docker Volume to a PostgreSQL Container

To mount a Docker volume to a PostgreSQL container, we need to follow a few simple steps. We assume that Docker and PostgreSQL are already installed on your system.

Step 1: Create a Docker Volume

To create a Docker volume, run the following command:

docker volume create my_volume

Step 2: Start the PostgreSQL Container

To start the PostgreSQL container and mount the Docker volume to it, run the following command:

docker run --name postgres -e POSTGRES_PASSWORD=mysecretpassword -d -v my_volume:/var/lib/postgresql/data postgres

In this command, we have specified the following parameters:

  • --name postgres: This specifies the name of the container.
  • -e POSTGRES_PASSWORD=mysecretpassword: This sets the password for the PostgreSQL user.
  • -d: This runs the container in the background.
  • -v my_volume:/var/lib/postgresql/data: This mounts the Docker volume my_volume to the /var/lib/postgresql/data directory inside the container.
  • postgres: This specifies the name of the Docker image to use.

Step 3: Connect to the PostgreSQL Container

To connect to the PostgreSQL container, run the following command:

docker exec -it postgres psql -U postgres

This command will open the PostgreSQL command-line interface. You can now create tables and perform other database operations.

More Examples

To mount multiple Docker volumes to a PostgreSQL container, you can specify multiple -v parameters in the docker run command. For example, to mount two Docker volumes named my_volume1 and my_volume2, run the following command:

docker run --name postgres -e POSTGRES_PASSWORD=mysecretpassword -d -v my_volume1:/var/lib/postgresql/data1 -v my_volume2:/var/lib/postgresql/data2 postgres

You can also mount a Docker volume to a specific directory inside the container. For example, to mount the Docker volume my_volume to the /mydata directory inside the container, run the following command:

docker run --name postgres -e POSTGRES_PASSWORD=mysecretpassword -d -v my_volume:/mydata postgres

In this article, we have discussed how to mount a Docker volume to a PostgreSQL container. By following these simple steps, you can easily share data between containers or between a container and the host machine. Docker volumes provide a convenient way to store and manage persistent data in Docker containers.

Related Searches and Questions asked:

  • How to Install Portainer on RHEL 8
  • How to Install Portainer on CentOS 8
  • How to Install Podman on CentOS 8
  • How to Install Podman on Linux Mint
  • That's it for this post. Keep practicing and have fun. Leave your comments if any.

    Post a Comment

    0 Comments