Docker

Docker – Running PostgreSQL as a container

Docker simplifies database management, ensuring consistency across environments. Let us delve into a practical approach to understanding the management of PostgreSQL in a Docker container.

1. Introduction

Docker is an open-source platform used for the containerization of applications. It allows developers to package their applications along with their dependencies, libraries, and other necessary components into a single container that can run reliably and consistently on any platform. The containerization technology provided by Docker ensures that the application behaves the same way regardless of the underlying infrastructure. Some benefits of Docker are:

  • Portability: Docker containers can run on any platform, regardless of the underlying infrastructure. This makes it easy to move applications between development, testing, and production environments.
  • Scalability: Docker allows you to quickly and easily scale your application by adding or removing containers as needed, without having to make changes to the underlying infrastructure.
  • Isolation: Docker provides a high level of isolation between applications, ensuring that each container runs independently of others, without interfering with each other.
  • Efficiency: Docker containers are lightweight and efficient, consuming fewer resources than traditional virtual machines. This allows you to run more applications on the same hardware.
  • Consistency: Docker ensures that applications behave the same way across different environments, making it easier to test and deploy new versions of your application.
  • Security: Docker provides built-in security features that help protect your applications from external threats. Docker containers are isolated from each other and the underlying infrastructure, reducing the risk of attacks.

If someone needs to go through the Docker installation, please watch this video.

2. PostgreSQL in a Docker container

To set up PostgreSQL in Docker we will be making use of the docker-compose.yml file. A Docker Compose YML file is a configuration file that defines how Docker containers should behave when started as part of a multi-container application. It specifies services, networks, and volumes, along with their settings and relationships, simplifying the deployment and management of interconnected Docker containers.

2.1 Creating and Starting the Container

Let us break down the key components of the Docker Compose file (postgresql-docker-compose.yml) that will enable you to deploy a PostgreSQL container with specified configurations and persistent data storage.

  • Version: The version: '3.8' indicates the version of the Docker Compose file syntax being used.
  • Services: The services section defines the containers that make up the application.
    • image: postgres:latest: Specifies the Docker image to be used (latest version of PostgreSQL).
    • container_name: my-postgres-container: Assign a custom name to the PostgreSQL container.
    • restart: no: Specifies that the container should not automatically restart.
    • environment: Sets environment variables for PostgreSQL, including user, password, and database.
    • ports: - "5445:5432": Maps port 5445 on the host to port 5432 on the PostgreSQL container.
    • volumes: - postgres_data:/var/lib/postgresql/data: Mounts a volume named postgres_data to persist PostgreSQL data.
  • Volumes: The volumes section defines named volumes used by the services. postgres_data is a named volume used to store PostgreSQL data persistently.
version: '3.8'

services:
  postgres:
    image: postgres:latest
    container_name: my-postgres-container
    restart: no
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: mysecretpassword
      POSTGRES_DB: welcome
    ports:
      - "5445:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data

volumes:
  postgres_data:

2.1.1 Starting the Container

The docker-compose up command will read the configuration file, and create and start the service defined within it. The command will start the postgresql container with the given settings and expose it on a port 5445 of the host system while persisting data in a named volume called postgres_data.

docker-compose -f postgresql-docker-compose.yml up
Fig. 1: Initializing and starting the container

2.1.2 Verifying the Container

The docker ps -a command will list both running and stopped containers. We will use the filter attribute to display the postgresql container.

docker ps -a --filter "name=my-postgres-container"
Fig. 2: Verifying container

2.2 Accessing the Container Logs

The docker-compose logs command is useful for troubleshooting, monitoring, or simply observing the activities of the containers defined in the compose configuration.

docker-compose -f postgresql-docker-compose.yml logs
Fig. 3: Accessing the container logs

2.3 Accessing the Container

The docker exec command is useful in running an interactive shell inside the container. This command helps in troubleshooting, inspecting the container’s file system, or running specific commands.

docker exec -it my-postgres-container bash

Once you’re inside the container we can trigger some sql commands. You can download the SQL scripts from the Downloads section.

Fig. 4: Accessing the container and running SQL scripts

2.4 Stopping the Container

The docker-compose down command is useful to stop the container defined in the compose configuration.

docker-compose -f postgresql-docker-compose.yml stop
Fig. 5: Stopping the container

2.5 Re-running the Container

To re-run the container trigger the docker-compose up command.

docker-compose -f postgresql-docker-compose.yml up
Fig. 6: Re-run the container

2.6 Removing the Container

The docker-compose down command is useful for stopping and removing the containers, networks, and volumes defined in the compose configuration.

docker-compose -f postgresql-docker-compose.yml down
Fig. 7: Stopping and removing the container

3. Conclusion

In conclusion, leveraging PostgreSQL within a Docker container provides a flexible and efficient solution for database management. Docker’s containerization technology allows for the encapsulation of the PostgreSQL database, ensuring consistency across various environments and simplifying deployment processes. The ability to define configurations, set up environments, and manage dependencies using a Docker Compose file streamlines the deployment and scaling of PostgreSQL instances. Furthermore, Docker facilitates seamless collaboration by encapsulating the database and its dependencies, reducing compatibility issues. With the added benefit of easy portability, PostgreSQL in a Docker container enhances development workflows, making it a versatile choice for both local development environments and production deployments. The combination of PostgreSQL’s robust relational database capabilities and Docker’s containerization brings a powerful and convenient solution to modern database management challenges.

You can download the commands from the Downloads section.

4. Download the Code

This was a tutorial to download the commands used in this article.

Download
You can download the code of this example here: PostgreSQL in a Docker container

Yatin

An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button