简体   繁体   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. 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.

I've pinned the problem down up to this point in my database.provider.ts where the connection to the database via sequelize-typescript is made:到目前为止,我已经在我的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],
},];

The application container is starting up to this point: await sequelize.sync();应用程序容器正在启动到这一点: await sequelize.sync(); From there the app does not start further, so i guess something is blocking the connection to the database.从那里应用程序不会进一步启动,所以我猜想是什么阻止了与数据库的连接。 When i comment this part out, the app is starting (but of course without database-connection).当我注释掉这部分时,应用程序正在启动(但当然没有数据库连接)。 When i run it locally in development it works too.当我在开发中本地运行它时,它也可以工作。

Maybe my docker-compose.yml is relevant too:也许我的 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:

Solved this by switching from FROM node:latest to FROM node:lts in my Dockerfile.通过在我的 Dockerfile 中从FROM node:latest切换到FROM node:lts lts 解决了这个问题。 Memo to myself: Never pulling from latest again.给自己的备忘录:永远不要再从最新的东西拉出来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM