[英]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.