【问题标题】:App container not connecting properly to postgres database in deployment应用程序容器未正确连接到部署中的 postgres 数据库
【发布时间】:2020-06-12 18:54:42
【问题描述】:

TLDR:带有 PostgreSQL 数据库和 sequelize-typescript ORM 的 NestJs 服务器在开发中工作(通过在 Mac 上运行的 npm 和 postgres 数据库在本地启动),但在使用 docker-compose 构建和部署容器化时却没有。

到目前为止,我已经在我的 database.provider.ts 中将问题固定在了通过 sequelize-typescript 连接到数据库的位置:

export const databaseProviders = [
{
    provide: 'SEQUELIZE',
    useFactory: async (configService: ConfigService) => {
        const sequelize = new Sequelize(configService.sequelizeOrmConfig);
        sequelize.addModels([
            Model1,
            Model2,
            Model3,
        ]);
        await sequelize.sync();
        return sequelize;
    },
    inject: [ConfigService],
},];

应用程序容器正在启动到这一点:await sequelize.sync(); 从那里该应用程序不会进一步启动,所以我猜有些东西阻止了与数据库的连接。当我注释掉这部分时,应用程序正在启动(但当然没有数据库连接)。当我在本地开发中运行它时,它也可以工作。

也许我的 docker-compose.yml 也是相关的:

version: '3.5'

services:
  testname_app:
    container_name: testname_app
    image: registry.acme.com/test/test 
    env_file:
      - .env
    depends_on:
      - postgres
    links:
      - postgres:postgres
    networks:
      - testname-network
    restart: unless-stopped

  postgres:
    container_name: postgres
    image: postgres
    env_file: .env
    volumes:
      - pgdata:/var/lib/postgresql/data
    ports:
      - "5432:5432"
    networks:
      - testname-network
    restart: unless-stopped

networks:
  testname-network:
    driver: bridge

volumes:
  pgdata:

【问题讨论】:

    标签: postgresql docker docker-compose sequelize.js nestjs


    【解决方案1】:

    通过在我的 Dockerfile 中从 FROM node:latest 切换到 FROM node:lts 解决了这个问题。给自己的备忘录:永远不要再从最新版本中提取。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-13
      • 2023-04-02
      • 2019-09-04
      • 2023-03-31
      • 2022-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多