Having issues connecting Prisma with PostgresSQL on a Docker stack

I’m currently working on a NestJS backend application that is meant to integrate with a PostgreSQL database. I’ve created the following Dockerfile for the application:

FROM node:22 AS builder

WORKDIR /app

COPY package*.json ./
COPY prisma ./prisma/

RUN npm install

COPY . .

RUN npx prisma generate
RUN npm run build
RUN npx prisma migrate dev --name inits

# FROM node:22

# COPY --from=builder /app/node_modules ./node_modules
# COPY --from=builder /app/*.json ./
# COPY --from=builder /app/dist ./dist

EXPOSE 3000
CMD ["npm", "run", "dev"]

Alongside the following Docker Compose

services:
  postgres:
    image: postgres:latest
    container_name: my_postgres
    restart: always
    environment:
      POSTGRES_USER: mkyong
      POSTGRES_PASSWORD: password
      POSTGRES_DB: testdb
    ports:
      - "5432:5432"
    tty: true
  bravoqualy_back:
    container_name: bravoqualy_back
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - 3000:3000
    depends_on:
      - postgres
    env_file:
      - .env

The Prisma is meant to connect with the database using the following SQL string from the .env file:

DATABASE_URL="postgresql://mkyong:password@postgres:5432/testdb?schema=public"

When I first implemented this, things worked out as intended, but today when I tried using it suddenly the following error message showed up on the terminal:

[+] Building 10.5s (14/14) FINISHED                                                                         
 => [internal] load local bake definitions                                                             0.0s
 => => reading from stdin 588B                                                                         0.0s
 => [internal] load build definition from Dockerfile                                                   0.0s
 => => transferring dockerfile: 424B                                                                   0.0s
 => [internal] load metadata for docker.io/library/node:22                                             1.5s
 => [internal] load .dockerignore                                                                      0.0s
 => => transferring context: 54B                                                                       0.0s
 => [1/9] FROM docker.io/library/node:22@sha256:20a424ecd1d2064a44e12fe287bf3dae443aab31dc5e0c0cb6c74  0.0s
 => => resolve docker.io/library/node:22@sha256:20a424ecd1d2064a44e12fe287bf3dae443aab31dc5e0c0cb6c74  0.0s
 => [internal] load build context                                                                      0.2s
 => => transferring context: 20.78MB                                                                   0.2s
 => CACHED [2/9] WORKDIR /app                                                                          0.0s
 => CACHED [3/9] COPY package*.json ./                                                                 0.0s
 => CACHED [4/9] COPY prisma ./prisma/                                                                 0.0s
 => CACHED [5/9] RUN npm install                                                                       0.0s
 => [6/9] COPY . .                                                                                     0.2s
 => [7/9] RUN npx prisma generate                                                                      1.9s
 => [8/9] RUN npm run build                                                                            4.9s
 => ERROR [9/9] RUN npx prisma migrate dev --name inits                                                1.6s
------
 > [9/9] RUN npx prisma migrate dev --name inits:
1.272 Environment variables loaded from .env
1.273 Prisma schema loaded from prisma/schema.prisma
1.286 Datasource "db": PostgreSQL database "testdb", schema "public" at "postgres:5432"
1.286 
1.388 Error: P1001: Can't reach database server at `postgres:5432`
1.388 
1.388 Please make sure your database server is running at `postgres:5432`.
------
[+] up 0/1
 ⠙ Image bravoqualy_back-bravoqualy_back Building                                                     10.5s 
Dockerfile:14

--------------------

  12 |     RUN npx prisma generate

  13 |     RUN npm run build

  14 | >>> RUN npx prisma migrate dev --name inits

  15 |     

  16 |     # FROM node:22

--------------------

failed to solve: process "/bin/sh -c npx prisma migrate dev --name inits" did not complete successfully: exit code: 1

I tried changing the connection string from “postgres” to “localhost” but that didn’t do anything.

However, when I tried running the commands natively, outside the container, changing it to “localhost” lets Prisma connect to the database just fine, but “postgres” doesn’t.

I’m currently running this on a Ubuntu 24.04.4. NestJS is 11.0.1 and Prisma is 6.8.2.

Well, is the database already or still running? Did you try a ping from the app container to the database?

Nothing should use databases during build time. Since it is migration, you may want to move that part out to a kind of init container or an external script. To be honest, I’m not sure how it could work before. I never tried it so I’m not sure right now how the network would look like in build time and how that could access an already running database using a docker compose service reference. That would require the build container using the compose docker network bridge. And in order to work with “localhost”, the build container would need to use host network and connect to an already forwarded port.

In any case, you should really think about how you can move the migration out from the build process. Building images should only mean downloading and installing packages for the application, copying code and building application, not handling data and interacting with other containers in the same project.

You could also add the migration to the entrypoint and start with accessing the database. If it is empty, you start the migration. If you don’t want to connect to the database at the beginning, you can check the existence of a saved file that you save after the migration. And if the file is not there (on a volume), you start the migration and save the file.

1 Like