Docker -Compose -Prisma无法与数据库(Nestjs)连接

发布于 2025-02-13 12:30:56 字数 1453 浏览 0 评论 0原文

我正在尝试创建一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

執念 2025-02-20 12:30:56

您可能需要尝试在apiPostgres服务上设置网络。它会告诉Docker组成,该网络应在同一网络上,并且可以使他们相互交流。

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"
        networks:
            - my_network

    postgres:
        image: postgres:10.4
        ports:
            - "5432:5432"
        environment:
            POSTGRES_USER: user
            POSTGRES_PASSWORD: password
            POSTGRES_DB: db
        networks:
            - my_network

networks:
    my_network:
      driver: bridge

You may want to try to set networks on api and postgres services. It will tell Docker Compose that the should be on the same network and it will allow them to communicate with each other.

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"
        networks:
            - my_network

    postgres:
        image: postgres:10.4
        ports:
            - "5432:5432"
        environment:
            POSTGRES_USER: user
            POSTGRES_PASSWORD: password
            POSTGRES_DB: db
        networks:
            - my_network

networks:
    my_network:
      driver: bridge
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文