10

Technologies I use:

  • nestjs -> for backend
  • prisma -> for orm
  • postgresql -> database

I'm trying to run these technologies using Docker but I'm running into the following issue:

prisma schema loaded from prisma/schema.prism
Datasource "db": PostgreSQL database "nestjs", schema "public" at "localhost:5200"
Error: P1001: Can't reach database server at `localhost`:`5200`
Please make sure your database server is running at `localhost`:`5200`

docker-compose.dev.yml

version: '3.7'
services:
  db:
    image: postgres:12.9
    ports:
      - 5200:5432
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: 123
      POSTGRES_DB: nestjs
    volumes:
      - database-data:/var/lib/postgresql/data
    networks:
      - sai
    restart: always

  test:
    container_name: test
    image: test
    build:
      context: .
      target: development
      dockerfile: Dockerfile
    command: npm run start:prod
    ports:
      - 3000:3000
      - 9229:9229
    networks:
      - sai
    volumes:
      - .:/usr/src/app
      - /usr/src/app/node_modules
    links:
      - db
    depends_on:
      - db
    restart: always

networks:
  sai:
    driver: bridge

volumes:
  database-data:

Nestjs does not see my locahost database on port 5200.

Dockerfile file:

FROM node:latest as development
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install --only=development
COPY . .
RUN npm run build


FROM node:latest as production
ARG NODE_ENV=production
ENV NODE_ENV=${NODE_ENV}
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install --only=production
COPY . .
COPY --from=development /usr/src/app/prisma ./prisma
COPY --from=development /usr/src/app/dist ./dist
EXPOSE 3000
CMD npm run start:prod

The npm run start:prod command also corresponds to the following in the package.json file:

...
  "generate:prisma": "npx prisma migrate dev --name init",
  "start:prod": "npm run generate:prisma && npm run dist/main",
...
Furkan Gulsen
  • 1,384
  • 2
  • 12
  • 24

4 Answers4

12

Instead of using localhost:5200 as the address of the database, you need to use db:5432.

Localhost and the mapped port are used when connecting from the host machine. You're connecting from another container on the bridge network and there you need to use the service name and the port the container is listening on.

Hans Kilian
  • 18,948
  • 1
  • 26
  • 35
  • 2
    I did what you said. This time I'm getting an error like this: Error: P1001: Can't reach database server at `db`:`5200` – Furkan Gulsen Feb 24 '22 at 12:34
  • 2
    You need to use port 5432. Not 5200. – Hans Kilian Feb 24 '22 at 12:35
  • Yeah yeah. it worked now. Thank you – Furkan Gulsen Feb 24 '22 at 12:43
  • 3
    It didn't work for me. I tried doing ```db:5432 ``` also. I also tried adding ```?connect_timeout=300``` as per some comments. But none of them worked for me. Did anyone resolved this issue – Subash Shrestha Aug 18 '22 at 19:35
  • @SubashShrestha the name can vary depending on how you named your database container. In my case, I the database as `database` instead of `db`. So, in my case, I have to use `database:5432`. And of course, don't forget to use docker-compose tag `depends_on` – Ugnius Malūkas Aug 23 '22 at 10:32
  • Yeah, I named the container as postgredb so I needed to use the same name for database URL too. Now it worked. – Subash Shrestha Aug 23 '22 at 11:15
1

Instead of using localhost:5200 as the address of the database, you need to use db:5432.

remomiendo la salucion de Hans Kilian

olmedo
  • 11
  • 1
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 26 '22 at 09:35
0

In the docker compose I found:

services:
  db-test:
    image: postgres:15.2
    ports:
      - "5433:5432"

and i connected to the db using: localhost:5433

asma
  • 599
  • 1
  • 7
  • 19
0

In my case:

  • in the docker-compose file for Windows you need to use host: host.docker.internal and exposed port in my case 5433: (DATABASE_URL=postgresql://postgres:postgres@host.docker.internal:5433/user-accounts)
version: '3.1'

services:
  postgres:
    image: postgres:15
    restart: always
    environment:
      - POSTGRES_PASSWORD=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_DB=user-accounts
      - POSTGRES_HOST_AUTH_METHOD=trust
    ports:
      - '5433:5432'
    volumes:
      - ./docker-data/user-accounts:/var/lib/postgresql/data
    healthcheck:
      test: [ "CMD-SHELL", "pg_isready -U postgres" ]
      interval: 5s
      timeout: 5s

  server:
    build:
      context: .
      dockerfile: Dockerfile
    command: sh -c "npx prisma db push && npm start"
    environment:
      - PORT=3000
      - JWT_SECRET=super_secret
      - DATABASE_URL=postgresql://postgres:postgres@host.docker.internal:5433/user-accounts
    ports:
      - '3000:3000'
    depends_on:
      postgres:
        condition: service_healthy
  • in the docker-compose file for Linux (only database url changed!) you need to use host: postgres which is the name of my service and port: 5432 (internal port) (DATABASE_URL=postgresql://postgres:postgres@postgres:5432/user-accounts)
version: '3.1'

services:
  postgres:
    image: postgres:15
    restart: always
    environment:
      - POSTGRES_PASSWORD=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_DB=user-accounts
      - POSTGRES_HOST_AUTH_METHOD=trust
    ports:
      - '5433:5432'
    volumes:
      - ./docker-data/user-accounts:/var/lib/postgresql/data
    healthcheck:
      test: [ "CMD-SHELL", "pg_isready -U postgres" ]
      interval: 5s
      timeout: 5s

  server:
    build:
      context: .
      dockerfile: Dockerfile
    command: sh -c "npx prisma db push && npm start"
    environment:
      - PORT=3000
      - JWT_SECRET=super_secret
      - DATABASE_URL=postgresql://postgres:postgres@postgres:5432/user-accounts
    ports:
      - '3000:3000'
    depends_on:
      postgres:
        condition: service_healthy

GitHub: https://github.com/KhitrovMaksim/user-accounts

helvete
  • 2,455
  • 13
  • 33
  • 37