Docker、Postgres、Liquibase:无法创建连接

问题描述 投票:0回答:1

我正在尝试通过 docker 中的 liquibase 在 postgres 上运行迁移。我收到连接拒绝错误。但是,我可以使用相同的凭据通过 pgAdmin 连接到在 docker 容器中运行的 postgres 数据库。我查看了堆栈溢出和谷歌,但未能解决问题。错误如下所示:

app-liquibase  | Liquibase Version: 4.30.0
app-liquibase  | Liquibase Open Source 4.30.0 by Liquibase
app-liquibase  | ERROR: Exception Details
app-liquibase  | ERROR: Exception Primary Class:  ConnectException
app-liquibase  | ERROR: Exception Primary Reason:  Connection refused
app-liquibase  | ERROR: Exception Primary Source:  4.30.0
app-liquibase  | 
app-liquibase  | Unexpected error running Liquibase: Connection could not be created to jdbc:postgresql://localhost:5432/shopping with driver org.postgresql.Driver.  Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
app-liquibase  |   - Caused by: Connection refused
app-liquibase  | 
app-liquibase  | For more information, please use the --log-level flag
app-liquibase exited with code 1
Aborting on container exit...

这是我的 docker-compose 文件

services:
  app:
    build: ./
    container_name: app-server
    volumes:
      - ./:/app-server
    ports:
      - 8080:8080
    expose:
      - 8080
    networks:
      - ktor-network
    depends_on:
      - database
  database:
    image: postgres:latest
    container_name: app-db
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: mydb
    ports:
      - 5432:5432
    expose:
      - 5432
    networks:
      - ktor-network
    healthcheck:
      test: [ "CMD-SHELL", "pg_isready -U postgres" ]
      interval: 5s
      timeout: 5s
      retries: 5
  database-migration:
    image: liquibase/liquibase:latest
    container_name: app-liquibase
    volumes:
      - ./app/src/main/resources/liquibase/:/liquibase/changelog
    command: ["--url=jdbc:postgresql://localhost:5432/mydb", "--username=postgres", "--password=postgres", "--changelog-file=liquibase/changelog/changelog.xml", "update"]
    depends_on:
      database:
        condition: service_healthy
    networks:
      - ktor-network
networks:
  ktor-network:
    driver: bridge
postgresql docker liquibase
1个回答
0
投票

这里的麻烦很常见,年复一年每天都在问这个问题,而答案仍然是一样的:

--url=jdbc:postgresql://localhost:5432/mydb
- 您在这里使用
localhost
代替服务/容器名称,这是错误的。只需在此处使用服务或容器名称而不是 localhost。

© www.soinside.com 2019 - 2024. All rights reserved.