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)

Most upvoted comments

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 localhost on inside you application docker container. So please use postgres(the container name) instead of localhost as 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:

version: '3.8'
services:
  postgres:
    image: postgres:latest
    restart: always
    container_name: pg
    #tty: true
    ports:
      - '5432:5432'
    volumes:
      - data:/var/lib/postgresql/data
    network_mode: 'host'
    environment:
      POSTGRES_USER: prisma
      POSTGRES_PASSWORD: prisma
      POSTGRES_DB: prisma
    expose:
      - 5432
volumes:
  data:
    external: true

If I do docker-compose up -d and docker exec -it {container_id} psql -d prisma -U prisma -W I can get in just fine.

It works. If I use npx prisma migrate dev --preview-feature I get:

Environment variables loaded from .env
Prisma schema loaded from prisma/schema.prisma
Datasource "db": PostgreSQL database "prisma", schema "public" at "postgres:5432"

Error: P1001: Can't reach database server at `postgres`:`5432`

Please make sure your database server is running at `postgres`:`5432`.

My DATABASE_URL is DATABASE_URL=postgresql://prisma:prisma@postgres:5432/prisma. I’ve tried changing @postgres pg or localhost or to anything else that’s been pointed out here. Doesn’t work. Any help please? I’ve tried solving this for 2 days.

# docker-compose.yml
version: '3.1'
services:
  postgres:
    image: postgres:latest
    ports:
      - "5432:5432"
    env_file:
      - database.env # configure postgres
    volumes:
      - ./data/postgres:/var/lib/postgresql/data

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:

I’ve got the answer you are all waiting for… Docker starts services by alphabetical order!!!! DUMMEST THING EVER! If you want to fix this like I did… just put a ‘z’ in front of the service you want to run last, or an a in front of the service you want to run first… they should really just add an order field to this… your code fails because your app service is called ‘app’ which runs first… and your db is called ‘postgres’ which means it will run after…

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/prisma/prisma/issues/1385#issuecomment-696480705, or unsubscribe https://github.com/notifications/unsubscribe-auth/AKYIEUSZWQLY4QKCANDK2W3SHAFSHANCNFSM4KJPDKSA .

@harmnot

in my .env DB_URI = postgres://username:s@localhost:5555/prod for my schema.prisma

You must use the service’s name instead of localhost. Based on your docker-compose.yaml, it should be postgres:5555.

# .env
DB_URI="postgres://username:s@postgres:5555/prod"

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"

# docker-compose.yml
version: "3"
services:
  database:
    image: "postgres"
    env_file:
      - database.env # Or your variables
    network_mode: "host"