Looks like someone had a similar question recently #13012 which helped me solve the above issue!
Basically, in the local env file, ensure you use localhost as the hostname
DATABASE_URL="postgresql://nest-twitter:password@localhost:5432/nest-twitter-db?schema=public"
This means when you run npx prisma studio it connects to the local postgres instance in Docker with no issues.
However, this will then break the Docker services from working together as you need to use postgres as the hostname.
So what you can do in the docker-compose.yml file is define an overriding DATABASE_URL environment variable, using postgres as the hostname
version: "3.9"
services:
api:
build:
dockerfile: Dockerfile
context: .
# Only will build development stage from our dockerfile
target: development
# TBC
volumes:
- .:/usr/src/app
- ./usr/src/app/node_modules
# Run in dev Mode: npm run start:dev
command: npm run start:dev
depends_on:
- postgres
env_file:
- .env
environment:
- DATABASE_URL=postgresql://nest-twitter:password@postgres:5432/nest-twitter-db?schema=public
ports:
- 3000:3000
postgres:
image: postgres
restart: always
environment:
POSTGRES_NAME: nest-twitter-db
POSTGRES_USER: nest-twitter
POSTGRES_PASSWORD: password
ports:
- '5432:5432'
volumes:
- nest-twitter-db:/var/lib/postgresql/data
volumes:
nest-twitter-db:
# 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.
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