0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

docker-composeのコンテナpostgresに対してWindowsのpgAdminから接続する

Posted at

dbtを学ぶ中で、WSL(Windows Sybsystem for Linux)でdocker-composeで使用してpostgresコンテナを立ち上げた。
ローカルの外部GUI環境からpostgresのサーバにアクセウするためのメモを残します。

docker-composeのymlファイル

version: '3'
services:
  postgres:
    image: postgres:latest
    restart: always
    ports:
      - 5432:5432
    environment:
      POSTGRES_USER: admin
      POSTGRES_PASSWORD: admin
    volumes:
      - pgdata:/var/lib/postgresql/data

  pgadmin:
    image: dpage/pgadmin4
    restart: always
    ports:
      - 5050:80
    environment:
      PGADMIN_DEFAULT_EMAIL: admin@admin.com
      PGADMIN_DEFAULT_PASSWORD: admin
    depends_on:
      - postgres

volumes:
  pgdata:
(venv) (base) user@DESKTOP-C0A2DE7:/mnt/d/iCloudDrive/MyJob/aboutTech/Github_repo/dbt_training$ docker-compose up -d
WARN[0000] /mnt/d/iCloudDrive/MyJob/aboutTech/Github_repo/dbt_training/docker-compose.yml: the attribute `version` is obsolete, it will be ignored, please remove it to avoid potential confusion
[+] Running 16/16
 ✔ pgadmin Pulled                                                                                                 28.0s
   ✔ 9824c27679d3 Pull complete                                                                                    2.2s
   ✔ 48d812ef7220 Pull complete                                                                                   21.5s
   ✔ b365d116b74d Pull complete                                                                                   21.6s
   ✔ 3b2350257df7 Pull complete                                                                                   21.6s
   ✔ af5b2b90fd54 Pull complete                                                                                   21.7s
   ✔ f69a69017cce Pull complete                                                                                   21.7s
   ✔ 41bdcaad13c5 Pull complete                                                                                   21.8s
   ✔ 550b406bf657 Pull complete                                                                                   21.8s
   ✔ 38a650e02953 Pull complete                                                                                   22.9s
   ✔ 5f8fa40f62df Pull complete                                                                                   23.3s
   ✔ 3b70849f20db Pull complete                                                                                   23.4s
   ✔ fae804a2bde9 Pull complete                                                                                   23.4s
   ✔ 787e7cac5f13 Pull complete                                                                                   23.4s
   ✔ 7f5ab96c11d2 Pull complete                                                                                   23.5s
   ✔ 4b3be4c74107 Pull complete                                                                                   24.7s
[+] Running 2/2
 ✔ Container dbt_training-postgres-1  Running                                                                      0.0s
 ✔ Container dbt_training-pgadmin-1   Started                                                                      1.1s

dockerのプロセスが動いています。

(venv) (base) user@DESKTOP-C0A2DE7:/mnt/d/iCloudDrive/MyJob/aboutTech/Github_repo/dbt_training$ docker ps
CONTAINER ID   IMAGE             COMMAND                  CREATED          STATUS          PORTS                                              NAMES
77b0f04a6e03   dpage/pgadmin4    "/entrypoint.sh"         14 minutes ago   Up 14 minutes   443/tcp, 0.0.0.0:5050->80/tcp, [::]:5050->80/tcp   dbt_training-pgadmin-1
e87cae7ffc06   postgres:latest   "docker-entrypoint.s…"   4 days ago       Up 4 days       0.0.0.0:5432->5432/tcp, :::5432->5432/tcp          dbt_training-postgres-1
(venv) (base) user@DESKTOP-C0A2DE7:/mnt/d/iCloudDrive/MyJob/aboutTech/Github_repo/dbt_training$

ここで引っ掛けですが、ローカルホストの80番ポートにアクセスしてもつながらないんですね。。
image.png

WSL上のネットワークイーサのIPアドレスを確認します。「172.30.18.234」ですね。

(venv) (base) user@DESKTOP-C0A2DE7:/mnt/d/iCloudDrive/MyJob/aboutTech/Github_repo/dbt_training$ ip addr show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 00:15:5d:11:ad:0e brd ff:ff:ff:ff:ff:ff
    inet 172.30.18.234/20 brd 172.30.31.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::215:5dff:fe11:ad0e/64 scope link
       valid_lft forever preferred_lft forever

「172.30.18.234:5050」で接続されます。(5050ポートがコンテナの80ポートに転送されるので、「172.30.18.234」ホストに対しては5050ポートでアクセスします)
image.png

ログインできました。
image.png

サーバから「登録」を設定するとコンテナ上で動いているpostgresにアクセスすることができました。
image.png

ちなみにこのようにテーブルをDROPすることもできる。
image.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?