Containers are a lightweight form of virtualization that allows you to run applications and their dependencies in isolated environments. Unlike traditional virtual machines (VMs) that require a full OS for each instance, containers share the host OS kernel and isolate the application processes. This results in faster start-up times, reduced overhead, and improved resource efficiency.
Benefits of Containers
Portability: Containers encapsulate an application and its dependencies, making it easy to move across different environments (development, testing, production) without worrying about compatibility issues.
Scalability: Containers can be easily scaled up or down based on the application demand.
Isolation: Containers provide process and file system isolation, ensuring that applications do not interfere with each other.
Consistency: Containers ensure that the application runs the same regardless of where it is deployed, reducing “works on my machine” issues.
Docker: The Leading Container Platform
Docker is a popular platform that simplifies the process of creating, deploying, and managing containerized applications. It provides tools and utilities to work with containers, including the Docker Engine for running containers and the Docker Hub for storing and sharing container images.
Setting Up PostgreSQL on Docker
In this guide, we will walk you through the steps to set up PostgreSQL on Docker, including creating and managing containers, volumes, and connecting to the PostgreSQL database.
Prerequisites
- A system with yum package manager (e.g., RHEL-based distribution).
sudo privileges for installing and configuring Docker.
Step 1: Install Docker
First, we need to add the Docker repository and install Docker on our system.
sudo yum-config-manager --add-repo https://download.docker.com/linux/rhel/docker-ce.repo
Next, install Docker and its components:
sudo yum install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
Step 2: Start Docker Service
After the installation, start the Docker service and enable it to start on boot.
sudo systemctl start docker
sudo systemctl enable docker
Step 3: Pull the PostgreSQL Docker Image
Docker images are pre-packaged environments that include all necessary dependencies to run an application. To pull the PostgreSQL image from Docker Hub, use the following command:
docker pull postgres
Step 4: Create a Docker Volume
Docker volumes are used to persist data outside of the container’s lifecycle. We will create a volume to store PostgreSQL data.
docker volume create cluster_postgres
Step 5: Run the PostgreSQL Docker Container
Now, let’s run the PostgreSQL container using the image we pulled earlier. We’ll also link the volume we created to persist data.
docker run --name postgres_container -e POSTGRES_PASSWORD=test123 -d -p 5432:5432 -v cluster_postgres:/var/lib/postgresql/data postgres
In this command:
--name postgres_container assigns a name to the container.-e POSTGRES_PASSWORD=test123 sets the password for the PostgreSQL superuser.-d runs the container in detached mode.-p 5432:5432 maps the host port 5432 to the container port 5432.-v cluster_postgres:/var/lib/postgresql/data mounts the Docker volume to the PostgreSQL data directory inside the container.
Step 6: Verify the Container is Running
To ensure that the PostgreSQL container is running, use the following command:
docker ps
This command lists all running containers. You should see your
The use of containers offers numerous benefits such as portability, scalability, isolation, and consistency. By leveraging Docker, businesses can simplify their deployment processes while ensuring efficient resource utilization. Setting up PostgreSQL on Docker is a straightforward process that underscores these advantages, making it an attractive option for modern application development and deployment strategies.