首页 > 解决方案 > 连接到 docker 中托管的 postgresql 时出现问题

问题描述

我在连接到 docker 中托管的 postgresql 数据库时出现以下错误。

myapp_Container | WARNING - ConnectionBus: Database connections: 0 active, 0 idle.
myapp_Container | ERROR - ConnectionBus: Opening JDBC connection to Some(db:54325) failed with SQLState: 08001 Error code: 0 Message: The connection attempt failed., giving up...(4/4)
myapp_Container | WARNING - ConnectionBus: Cannot create database connection.

我有两个 docker-compose 文件如下

APP Docker-撰写文件

version: "3"
services:
  app:
    image: myapp
    container_name: myapp_Container
    ports:
      - "8081:8081"
    network_mode: postgres_net
    environment:
      - ADMIN_PASSWORD=Password1!
      - DATABASE_ENDPOINT=postgres://user:passwd@db:54325/mydb
    external_links:
      - db:localpostgres_1

DB Docker-撰写文件

version: "3"

services:
  postgres:
    image: postgres
    container_name: localpostgres_1
    restart: always
    environment:
      - POSTGRES_USER=user
      - POSTGRES_PASSWORD=passwd
      - TZ=Europe/Amsterdam
    ports:
      - 54325:5432
    networks:
      - postgres_net

networks:
  postgres_net:
    external: true

我通过创建 postgres_netdocker network create -d bridge postgres_net

我为什么要这样做?我想创建具有相同 docker-compose 结构的应用程序,而不为每个应用程序创建一个 postgresql 数据库,如果我像下面这样将它们放在一起,它将起作用。

version: "3"
services:
  app:
    image: myapp
    container_name: myapp_Container
    ports:
      - "8081:8081"
    environment:
      - ADMIN_PASSWORD=Password1!
      - DATABASE_ENDPOINT=postgres://user:passwd@db/mydb
    links:
      - db

  db:
    image: postgres
    container_name: postgres_x
    ports:
      - "54320:5432"
    environment:
      - TZ=Europe/Amsterdam
      - POSTGRES_USER=user
      - POSTGRES_PASSWORD=passwd
    restart: always

我可以通过 pgadmin 连接到 Db,但不明白为什么 myapp 无法连接,请帮忙

pg_hba.conf


local   all             all                                     trust
# IPv4 local connections:
host    all             all             127.0.0.1/32            trust
# IPv6 local connections:
host    all             all             ::1/128                 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
local   replication     all                                     trust
host    replication     all             127.0.0.1/32            trust  

host    replication     all             ::1/128                 trust

host all all all md5

标签: postgresqldockerdocker-compose

解决方案


您尚未在 First compose 文件(App compose)中正确定义网络。您已指定postgres_net哪个network_mode: postgres_net无效。您需要像在第二个撰写文件中一样正确指定它。

更正的撰写文件将是:

APP Docker-Compose

version: "3"
services:
  app:
    image: myapp
    container_name: myapp_Container
    ports:
      - "8081:8081"
    environment:
      - ADMIN_PASSWORD=Password1!
      - DATABASE_ENDPOINT=postgres://user:passwd@db:54325/mydb
    networks:   # declare network here
      - postgres_net

networks: #define network
  postgres_net:
    external: true

DB Docker-Compose

version: "3"

services:
  postgres:
    image: postgres
    container_name: localpostgres_1
    restart: always
    environment:
      - POSTGRES_USER=user
      - POSTGRES_PASSWORD=passwd
      - TZ=Europe/Amsterdam
    ports:
      - 54325:5432
    networks:
      - postgres_net

networks:
  postgres_net:
    external: true

推荐阅读