Docker -Compose -Prisma无法与数据库(Nestjs)连接
我正在尝试创建一个docker-compose来运行后端和前端,以及同时运行数据库(PostgreSQL),但是当我运行时,我会遇到此错误:
/usr/src/app/node_modules/@prisma/client/runtime/index.js:44801
api_1 | reject(new PrismaClientInitializationError(error2.message, this.config.clientVersion, error2.error_code));
api_1 | ^
api_1 |
api_1 | PrismaClientInitializationError: Can't reach database server at `postgres`:`5432`
api_1 |
api_1 | Please make sure your database server is running at `postgres`:`5432`.
api_1 | at /usr/src/app/node_modules/@prisma/client/runtime/index.js:44801:20 {
api_1 | clientVersion: '3.15.2',
api_1 | errorCode: 'P1001'
api_1 | }
问题是将应用程序容器与数据库容器连接起来,但是我该如何修复?
我的docker-compose:
version: '3.8'
services:
api:
build: back-end/
depends_on:
- postgres
environment:
DATABASE_URL: postgres://user:password@postgres:5432/db
NODE_ENV: development
PORT: 3000
ports:
- "8080:3000"
postgres:
image: postgres:10.4
ports:
- "5432:5432"
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: password
POSTGRES_DB: db
.env在后端文件夹中(这是用于将Nestjs/Prisma与数据库连接):
DATABASE_URL="postgres://user:password@postgres:5432/db?schema=public"
I'm trying to create a docker-compose to run back-end and front-end, and database (PostgreSQL) at the same time but when I run it I get this error :
/usr/src/app/node_modules/@prisma/client/runtime/index.js:44801
api_1 | reject(new PrismaClientInitializationError(error2.message, this.config.clientVersion, error2.error_code));
api_1 | ^
api_1 |
api_1 | PrismaClientInitializationError: Can't reach database server at `postgres`:`5432`
api_1 |
api_1 | Please make sure your database server is running at `postgres`:`5432`.
api_1 | at /usr/src/app/node_modules/@prisma/client/runtime/index.js:44801:20 {
api_1 | clientVersion: '3.15.2',
api_1 | errorCode: 'P1001'
api_1 | }
the problem is connecting the application container with database container but how can I fix it?
my docker-compose:
version: '3.8'
services:
api:
build: back-end/
depends_on:
- postgres
environment:
DATABASE_URL: postgres://user:password@postgres:5432/db
NODE_ENV: development
PORT: 3000
ports:
- "8080:3000"
postgres:
image: postgres:10.4
ports:
- "5432:5432"
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: password
POSTGRES_DB: db
.env inside the back-end folder (this is for connecting nestjs/Prisma with the database):
DATABASE_URL="postgres://user:password@postgres:5432/db?schema=public"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能需要尝试在
api
和Postgres
服务上设置网络
。它会告诉Docker组成,该网络应在同一网络上,并且可以使他们相互交流。You may want to try to set
networks
onapi
andpostgres
services. It will tell Docker Compose that the should be on the same network and it will allow them to communicate with each other.