How to Use Postgres Docker Official Image

PostgreSQL is a well-known and free-to-use relational database system that offers many features and options for data management. The Postgres Docker Image is an official Docker image offered by the PostgreSQL community and maintained on Docker Hub. It enables users to easily set up and run a PostgreSQL database server within a Docker container.

This article will illustrate:

- How to Use Official Postgres Image in Docker?

- How to Use Official Postgres Image in Docker Compose File?

How to Use Official Postgres Image in Docker?

To use the official Postgres image in Docker, first, download the Postgres image from the cloud repository (Docker Hub) in the local system utilizing the “docker pull postgres” command. Then, run the Postgres container by executing the “docker run -d --name postgresCont -p 5432:5432 -e POSTGRES_PASSWORD=pass123 postgres” command. Lastly, verify the running container.

Try out the given-below steps to understand it better.

Step 1: Navigate to Docker Hub

Redirect to Docker Hub, and sign in or sign up to a Docker Hub account by providing the required email/username and password:

img

Step 2: Search for Postgres Docker Image

In the search box, search for the Postgres image and open it:

img

Step 3: Copy the “pull” Command

Copy the below-highlighted pull command of the Postgres image:

img

Moreover, users can also download the Postgres image with the specific tag:

img

Step 4: Pull/Download Postgres Docker Image

Run the copied command in the specific Windows terminal to download the Docker image:

docker   pull postgres
img

By doing that the Postgres Docker image has been downloaded.

Step 5: Verify Download Image

List all the docker images to ensure that the Postgres image has been downloaded:

docker images
img

It can be noticed that the official “Postgres” image has been downloaded successfully.

Step 6: Run Postgres Container Using Postgres Image

To create and run the Postgres container using the Postgres image, utilize the “docker run --name -d <cont-name> -p 5432:5432 -e POSTGRES_PASSWORD=<password> postgres” command:

docker run -d --name postgresCont -p 5432:5432 -e POSTGRES_PASSWORD=pass123 postgres

Here:

- “-d” flag specifies that the container should execute in the background.

- “--name” option is used to assign the name for the container i.e. “postgresCont”.

- “-p” assigns the port for the container i.e. “5432:5432”.

- “-e POSTGRES_PASSWORD” configures the password to be “pass123”.

- “postgres” is the official Docker image:

img

This command has built and started the Postgres container.

Step 7: Verify Executing Container

Ensure that the Postgres container is built and currently executing via the below-provided command:

docker ps
img

As you can see the “PostgresCont” container is efficiently executing.

How to Use Official Postgres Image in Docker Compose File?

To use the Postgres image in Docker compose file, first, make a “docker-compose.yml” file in the Visual Studio Code and specify the desired services in it. Then, build and execute the Postgres container using the “docker-compose up -d” command.

Explore the provided steps to gain a clearer understanding.

Step 1: Make Docker Compose File

Create a “docker-compose.yml” file in the Visual Studio Code and define the desired services in it. For instance, we have specified the following services:

version:   '3.8'
   services:
  postgres_db:
  image: postgres:latest
  container_name: PostgresCont 
  restart: always
  environment:
  - POSTGRES_USER=postgres
  - POSTGRES_PASSWORD=postgres123
  ports:
  - '5432:5432'
  volumes:
  - postgres_db:/var/lib/postgresql/data
   volumes:
  postgres_db:
  driver: local
img

Step 2: Run the Postgres Container

Execute the given command to start the Postgres services defined in the Docker compose file:

docker-compose up -d
img

The above command has created and started the Postgres container.

Step 3: Verification

List the running containers to ensure that the Postgres container is executing:

docker ps
img

It can be observed that the “PostgresCont” container is successfully executing.

Conclusion

The official Postgres Docker Image can be used in Docker and Docker compose files. To use the Postgres image in Docker, download the Postgres image from the cloud repository. Then, run the Postgres container using the Postgres image and verify the running container. To use the Postgres image in Docker compose file, make a “docker-compose.yml” file in the Visual Studio Code and define the desired services in it. Then, start the compose service to build and execute the Postgres container. This article has illustrated the method of using the official Postgres Docker image.