簡體   English   中英

應用程序容器未正確連接到部署中的 postgres 數據庫

[英]App container not connecting properly to postgres database in deployment

TLDR: NestJs server with PostgreSQL database and sequelize-typescript ORM working in development (started locally via npm and postgres database running on mac) but not when builded and deployed containerized with 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:

通過在我的 Dockerfile 中從FROM node:latest切換到FROM node:lts lts 解決了這個問題。 給自己的備忘錄:永遠不要再從最新的東西拉出來。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM