1

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"
DarkSide77
  • 719
  • 1
  • 4
  • 21
  • Is it possible that the `api` is starting up before the database is fully ready; if you run `docker-compose up` in the foreground (without a `-d` option) are there database initialization messages after the Prisma error? If so, see [Docker Compose wait for container X before starting Y](https://stackoverflow.com/questions/31746182/docker-compose-wait-for-container-x-before-starting-y). – David Maze Jul 07 '22 at 01:24

1 Answers1

0

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
Marcos Leonel
  • 78
  • 3
  • 10