How to Run PostgreSQL and pgAdmin Using Docker(feat. Docker Engine, Docker-Compose V2)

Won Woo William Lyu·2022년 4월 16일
0

Docker

목록 보기
2/2
post-thumbnail
post-custom-banner

Before Starting

All the work in this article is based on WSL(Windows Subsystem for Linux).

  1. There are two versions for dokcer compose, V1 and V2.

    • The big differene between two versions is that V2 supports the compose command as part of Docker CLI, so use V2 if you don't know which one to use.
    • Clike Here, and follow instructions if you want to download V1.
    • I persoanlly use Docker Compose V2 and followed instructions from Here.
  2. You must run docker daemon to run PostgreSQL and pgAdmin using Docker.

Why Use Docker to Run PostgreSQL and pgAdmin?

You can locally install PostgreSQL and pgAdmin with below links.

However, there is a way of using PostgreSQL, pgAdmin without local installation, which is using a docker.

We will run uploaded PostgreSQL and pgAdmin images through a yaml file.

But before that, you must download docker-compose in order to run yaml file.

Prerequisite: Install Docker Compose V2

From below link, get latest release version of Docker Compose.
Docker Compose Version

  1. In terminal, Run below code to download current stable release of Docker Compose
    Currently, latest release version is 2.4.1

    
    $ DOCKER_CONFIG=${DOCKER_CONFIG:-$HOME/.docker}
    $ mkdir -p $DOCKER_CONFIG/cli-plugins
    $ curl -SL https://github.com/docker/compose/releases/download/v2.2.3/docker-compose-linux-x86_64 -o $DOCKER_CONFIG/cli-plugins/docker-compose
    
  2. Apply executable permissions to the binary.

    $ chmod +x $DOCKER_CONFIG/cli-plugins/docker-compose
  3. Test installation by printing version

    
    $ docker compose version
    > Docker Compose version v2.4.1

Run PostgreSQL and pgAdmin

Code

docker-compose.yml

version: "3"

services:
  db:
    container_name: test_db
    image: postgres
    environment:
      POSTGRES_USER : test
      POSTGRES_PASSWORD : 1234
      POSTGRES_DB : test_db
    ports:
     - "5432:5432"
  pgadmin:
    container_name: test_pgadmin
    image: dpage/pgadmin4
    restart: always
    environment:
      PGADMIN_DEFAULT_EMAIL : user@test.com
      PGADMIN_DEFAULT_PASSWORD: 1234
    ports:
      - "5050:80"
    depends_on:
      - db

Above is simple code which will run pgadmin and postgres.

Run and Connect

Now run the below command where docker-compose.yml is located.

$ docker compose up

If you successfully composed above code, you will be able to access pgadmin with localhost:5050

If the website looks like below picutre, Congrautations! you have successfully run pgAdmin with docker.

However, It is not done yet since you have to connect postgres db to pgAdmin.

Login pgAdmin with emil and password that is written in pgadmin section of yaml file which are user@test.com and 1234

After login, Click Servers / Register / Server
Server

In General, Name anything you want to name the server.

In Connection, write container_name of db on Host name and fill out username and password.

Tip : Host name / address should be filled with IP + port number of database if you connect local installed pgAdmin and Postgres db. However, docker provides IP + port as container name if you run both program through the docker.

If you are successfully connected pgAdmin with Postgres DB, you will see the database under the server like below picture.
DONE

profile
Hello!! I am an ENFP Backend Engineer!!
post-custom-banner

0개의 댓글