EpsilonCode
EpsilonCode

Reputation: 29

How to connect Prisma ORM running inside a container to a PostgreSQL database inside another container using docker-compose

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:-

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

Versions

Upvotes: 0

Views: 4734

Answers (2)

It appears that Prisma is experiencing difficulty connecting to your PostgreSQL database. I encountered a similar issue documented on Stack Overflow, which indicated that Docker might be facing challenges connecting to your locally hosted database. Another potential issue could be related to PostgreSQL configuration settings, specifically, whether you have enabled connections from all hosts in your PostgreSQL configuration. Link: Connecting to Postgresql in a docker container from outside

Upvotes: 0

erik258
erik258

Reputation: 16295

# 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.

Upvotes: 4

Related Questions