prisma: Can't reach database server which run in Docker
I currently try run Postgresql in Docker, and use that database in prisma2
in my docker list, my Postgresql running on localhost:5555 and I connecting it on DBeaver, it was successfully , after i run prisma2 app with Postgresql in docker-compose
it does’t work,
my docker-compose.yml like this
version: "3"
services:
app:
env_file:
- .env
build:
context: .
dockerfile: Dockerfile
links:
- postgres
ports:
- "8100:8100"
postgres:
container_name: postgres
image: postgres:10-alpine
ports:
- "5555:5432"
environment:
POSTGRES_USER: username
POSTGRES_PASSWORD: mypassword
POSTGRES_DB: prod
in my .env DB_URI = postgres://username:s@localhost:5555/prod for my schema.prisma
and after I run my docker-compose I got error like this :
Error: P1001: Can't reach database server at `localhost`:`5555`
Please make sure your database server is running at `localhost`:`5555`.
thought my Postgresql already running on that localhost:5555
is there any way to resolve this ?
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 27 (7 by maintainers)
I think this issue should be closed because lacking docker basic network knowledge is the cause of the problem in the first place. If you want to ask a question about Docker networking, you can do it on stackoverflow or to learn.
Localhost inside docker is bound to the container, not the machine. So you can’t access your postgres container via
localhoston inside you application docker container. So please usepostgres(the container name) instead oflocalhostas a host in order to connect to your database instance.If you still want to access your machines loopback interface inside docker for some reason, you can use
host.docker.internal(It doesn’t work on linux though). But I would highly recommend you to use your container name as the database host.I’m having a similar issue on probably a different setup. I’m running everything on WSL, have no other part of my application dockerized, just the database so it’s accessed by prisma. This is my
docker-compose.yml:If I do
docker-compose up -danddocker exec -it {container_id} psql -d prisma -U prisma -WI can get in just fine.It works. If I use
npx prisma migrate dev --preview-featureI get:My DATABASE_URL is
DATABASE_URL=postgresql://prisma:prisma@postgres:5432/prisma. I’ve tried changing@postgrespgorlocalhostor to anything else that’s been pointed out here. Doesn’t work. Any help please? I’ve tried solving this for 2 days.https://github.com/softmarshmallow/robbin/tree/master/server
https://github.com/softmarshmallow/robbin/blob/master/server/docker-compose.yml
database.env
here is my example for running postgres on doecker. had same issue, it works but, your docker postres will be generated under poject root.
still working on it.
DOCKERFILE
ADD https://github.com/ufoscout/docker-compose-wait/releases/download/2.7.3/wait /wait RUN chmod +x /wait
CMD /wait && npm run start
docker-compose.yml backend: container_name: backend restart: always depends_on: - mysql environment: WAIT_HOSTS: mysql:3306
On Mon, Sep 21, 2020 at 10:44 PM Chad Vandermark notifications@github.com wrote:
@harmnot
You must use the service’s name instead of
localhost. Based on yourdocker-compose.yaml, it should bepostgres:5555.This should work. Let me know whatever.
I wrote up a quick workaround for a related question regarding Prisma Studio which might be helpful for some use cases https://github.com/prisma/studio/issues/650#issuecomment-887801807
to fix you need to config your docker-compose with network_mode: “host” https://stackoverflow.com/a/55270528
Example of working configuration:
DATABASE_URL="postgresql://USER:PWD@localhost:5432/DB"