As the popularity of containerization continues to rise, Docker has become a go-to tool for developers and system administrators alike. One of the many advantages of Docker is its ability to efficiently manage and deploy databases in isolated environments. In this article, we will walk through how to setup or create a PostgreSQL database in Docker with docker-compose, allowing you to easily manage and scale your database operations.
If you’re looking to set up MongoDB within a Docker environment, this article provides a comprehensive guide to assist you in the configuration process.
Table of Contents
How to Create / Setup PostgreSQL Database in Docker
Before we begin, ensure that you have Docker installed on your system. You can download and install Docker from the official website (https://www.docker.com/). Once installed, make sure Docker is running.
Let’s start the step-by-step process to configure the PostgreSQL database in Docker.
Create a Docker Compose Script
Create a Docker Compose script to retrieve the latest or a specified version of the PostgreSQL image from the Docker Cloud registry. Specify an initial database name, username, and password for the initial setup upon database initialization. Utilize these credentials for subsequent database connections once the configuration is complete. Additionally, define the port on which the database will listen for incoming connections.
# // docker-compose.yml
Let us systematically analyze each attribute employed in the Docker Compose script to gain a comprehensive understanding of their respective roles.
container_name: Facilitates seamless identification and management.
image: Specifies the Docker image for PostgreSQL.
restart: Configures the container to restart always, thereby enhancing system robustness by automatically recovering from unexpected failures.
ports: Establishes a mapping between the host machine’s port 5432 and the container’s port 5432.
volumes: Enacts the mounting of the local directory
./.postgres-cacheinto the container’s
/var/lib/postgresql/datadirectory in a delegated fashion. This safeguards data persistence, even in scenarios where the container undergoes halts or removals.
environment: Defines pivotal environment variables for PostgreSQL:
POSTGRES_PASSWORD: Specifies the password for the PostgreSQL user.
POSTGRES_DB: Declares the name of the default database to be instantiated.
POSTGRES_USER: Sets the PostgreSQL username.
PGDATA: Overrides the default data directory to
Start the Docker Container
Having successfully crafted a Docker Compose script, the next step entails initiating the PostgreSQL Docker container.
Execute the following command to initiate the container build and launch process.
docker compose up --build -d db
Upon execution, the container will initiate, and you will observe an output resembling the following.
Connect PostgreSQL Database
Now that the container has been initiated, it is time to establish a connection to the PostgreSQL database using a database browser client. In this instance, I will connect to the database using DBeaver, a reputable database browser client.
Utilize the credentials that were specified during the creation of the Docker Compose script while establishing the connection.
Congratulations! You’ve successfully set up a PostgreSQL database in a Docker container and connected to it through a database browser client DBeaver.
Compose a Docker Compose script tailored to your specific requirements, proceed to build and initiate the container, and subsequently access the database seamlessly from your application. The process is straightforward and streamlined.
If in case your Docker behaves unexpectedly and fails to initiate, refer to this article for potential solutions.It offers insights that may assist in resolving any issues and extricating yourself from a potentially stalled situation.