Home > Blockchain >  How to connect Prisma ORM running inside a container to a PostgreSQL database inside another contain
How to connect Prisma ORM running inside a container to a PostgreSQL database inside another contain

Time:10-03

Description

I built a simple RestAPI using node.js and Prisma as an ORM and created a Dockerfile for that application, then i used docker-compose to make these services:-

  • web -> this is the application service, built using the Dockerfile mentioned above.
  • pgdb -> a postgresql container built using the PostgreSQL official docker image.

The Problem

when i try to run docker-compose up it fails to build the web service because Prisma can't run it's migrations due to it's inability to connect to the database that's running in the other service, namely pgdb.

The Specific Error Message

Error: P1001: Can't reach database server at pgdb:5432.
Please make sure your database server is running at pgdb:5432.

note: i used pgdb (the name of the PostgreSQL service) here to reference the host.

Expected Behavior

it should be able to use pgdb as a host reference and successfully connects to the database server located at pgdb:5432

Code

  • The Dockerfile referenced inside the docker-compose to create the web service

    FROM node:16
    
    WORKDIR /app
    
    COPY package*.json .
    
    RUN npm install
    
    COPY . .
    
    # Run Prisma migrations.
    RUN npx prisma migrate dev --name init
    
    # Compile typescript code to javascript.
    RUN npx tsc
    
    # Run the compiled code (compiled files are outputed to `dist` folder).
    CMD [ "node", "dist/index.js" ]
    
  • The docker-compose.yml

    version: "3.9"
    services:
      webapp:
        build: .
        ports:
          - "5000:5000"
        depends_on:
          - pgdb
      pgdb:
        image: postgres:14
        environment:
          - POSTGRES_USER=postgres
          - POSTGRES_PASSWORD=postgres
          - POSTGRES_DB=mydb
    
  • The .env (containing Prisma DB connection string)

    DATABASE_URL="postgresql://postgres:postgres@pgdb:5432/mydb?schema=public"
    

Versions

  • Node v16.17.0
  • Prisma ^4.4.0

CodePudding user response:

# Run Prisma migrations.
RUN npx prisma migrate dev --name init

it doesn't make sense to run migrations at docker image build time. The migrations should be run within the entrypoint of the image, not when its being built.

Then when the container is deployed, the migrations run before the application is started in each environment in which it's deployed.

  • Related