【问题标题】:Docker compose with NestJS PostgreSQL not connectingDocker 与 NestJS PostgreSQL 组合未连接
【发布时间】:2023-02-12 14:37:50
【问题描述】:

我曾尝试使用 PostgreSQL 对 NestJS 应用进行 dockerize。 Postgres 拒绝连接并且还显示了一个日志 database system was shut down at <<some timestamp>>。这是 docker-compose.yml 和日志。

version: '3'
services:
  postgres:
    image: postgres
    restart: always
    volumes:
      - ./pgdata:/var/lib/postgresql/data
    ports:
      - '5432:5432'
    environment:
      - POSTGRES_DB=gm
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
  pgadmin:
    image: dpage/pgadmin4
    environment:
      - PGADMIN_DEFAULT_EMAIL=admin@gmail.com
      - PGADMIN_DEFAULT_PASSWORD=admin
      - PGADMIN_LISTEN_PORT=5050
    ports:
      - "5050:5050"
  api:
    image: gm-server
    build:
      dockerfile: Dockerfile
      context: .
    volumes:
      - .:/home/node
    ports:
      - '8081:4001'
    depends_on:
      - postgres
    env_file: .env
    command: npm run start:prod
volumes:
  pgdata:
server-postgres-1  | PostgreSQL Database directory appears to contain a database; Skipping initialization
server-postgres-1  | 
server-postgres-1  | 2023-01-04 09:36:45.249 UTC [1] LOG:  starting PostgreSQL 15.0 (Debian 15.0-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
server-postgres-1  | 2023-01-04 09:36:45.250 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
server-postgres-1  | 2023-01-04 09:36:45.250 UTC [1] LOG:  listening on IPv6 address "::", port 5432
server-postgres-1  | 2023-01-04 09:36:45.255 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
server-postgres-1  | 2023-01-04 09:36:45.261 UTC [29] LOG:  database system was shut down at 2023-01-04 09:36:27 UTC
server-postgres-1  | 2023-01-04 09:36:45.274 UTC [1] LOG:  database system is ready to accept connections
server-api-1       | 
server-api-1       | > nestjs-app@0.0.1 start:prod
server-api-1       | > node dist/main
server-api-1       | 
server-api-1       | [Nest] 19  - 01/04/2023, 9:36:47 AM     LOG [NestFactory] Starting Nest application...
server-api-1       | [Nest] 19  - 01/04/2023, 9:36:47 AM     LOG [InstanceLoader] MulterModule dependencies initialized +61ms
server-api-1       | [Nest] 19  - 01/04/2023, 9:36:47 AM     LOG [InstanceLoader] MulterModule dependencies initialized +1ms
server-api-1       | [Nest] 19  - 01/04/2023, 9:36:47 AM     LOG [InstanceLoader] ConfigHostModule dependencies initialized +0ms
server-api-1       | [Nest] 19  - 01/04/2023, 9:36:47 AM     LOG [InstanceLoader] AppModule dependencies initialized +1ms
server-api-1       | [Nest] 19  - 01/04/2023, 9:36:47 AM     LOG [InstanceLoader] ConfigModule dependencies initialized +0ms
server-api-1       | [Nest] 19  - 01/04/2023, 9:36:47 AM   ERROR [ExceptionHandler] connect ECONNREFUSED 127.0.0.1:5432
server-api-1       | Error: connect ECONNREFUSED 127.0.0.1:5432
server-api-1       |     at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1300:16)
server-api-1 exited with code 1

而且我还尝试了大部分相关答案(在 Stackoverlow 将我标记为重复之前),但它们没有用。是的,我已经尝试按照之前的建议将主机更改为host.docker.internal

为了更清楚,这是我在 NestJS 中的 typeorm-datasource 配置

import { DataSource } from 'typeorm';

export const typeOrmConnectionDataSource = new DataSource({
  type: 'postgres',
  host: 'host.docker.internal',
  port: 5432,
  username: 'postgres',
  password: 'postgres',
  database: 'gm',
  entities: [__dirname + '/**/*.entity{.ts,.js}'],
  migrations: [__dirname + '/migrations/**/*{.ts,.js}'],
  logging: true,
  synchronize: false,
  migrationsRun: false,
});

为什么这个问题与其他“重复”问题不同?

这个问题不同的原因是因为

  1. 其他线程也没有解决问题。
  2. 即使我们认为他们这样做,解决方案对我也不起作用。

    更多证据?

【问题讨论】:

  • 如果应用程序和数据库位于同一个 docker compose 堆栈中(如您的示例),您可以使用别名 postgres 作为主机。尝试将数据源中的主机设置为postgres。让我知道它是否有效。
  • 感谢您的答复。我将数据源中的主机设置为postgres,但我仍然有同样的错误。
  • @generxhd 我认为这可能是 nestjs 错误,你怎么看?
  • 我不使用 typorm,但似乎忽略了 typeOrmConnectionDataSource,因为正如您在日志中看到的那样,typeorm 尝试连接到 localhost,这是默认值。
  • 我也尝试将它放入 AppModule 中,但它仍然被忽略了......

标签: postgresql docker docker-compose nestjs typeorm


【解决方案1】:

显然问题在于 NestJS 由于我对其脚本的自定义而无法正确编译。检查这是否是一个问题。

解决此问题后,只需按照说明使用“postgres”作为主机,它就会起作用(如果您遇到同样的问题)。

【讨论】:

    猜你喜欢
    • 2022-01-14
    • 2021-05-21
    • 1970-01-01
    • 2020-07-28
    • 2023-02-14
    • 2021-06-23
    • 2021-10-31
    • 2014-12-05
    • 2020-09-18
    相关资源
    最近更新 更多