skip to Main Content

I really dont know why but my Dockerfile image cannot access my postgres db running with docker compose. Im using Linux Ubuntu

// prisma.schema

generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

// package.json

"scripts": {
"build": "npx nest build",
"format": "prettier --write "src/*/.ts" "test/*/.ts"",
"start": "node dist/main",
"start:dev": "npm run start --watch",
"start:repl": "npm run start --entryFile repl",
"start:debug": "npm run start --watch --debug",
"lint": "eslint "{src,apps,libs,test}/*/.ts" --fix",
"test": "jest",
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./test/jest-e2e.json",
"prepare": "husky install",
"prisma:dev": "npx prisma migrate dev",
"prisma:reset": "npx prisma migrate reset",
"prisma:deploy": "npx prisma migrate deploy",
"prisma:studio": "npx prisma studio",
"docker:up": "docker compose up",
"docker:down": "docker compose down",
"docker:ls": "docker container ls"
},

// Dockefile

FROM node:18
WORKDIR /usr/src/app

COPY package*.json ./
RUN npm install

COPY . .

RUN npm run prisma:deploy
RUN npm run build

EXPOSE 8080

CMD ["npm", "run", "start:dev"]

// docker-compose.yml

version: '3'

services:
  postgres:
    image: postgres:15.3-alpine3.18
    restart: always
    environment:
      POSTGRES_USER: postgres
      POSTGRES_DB: personal
      POSTGRES_PASSWORD: pass123
    ports:
      - "5432:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data

  app:
    build: .
    depends_on:
      - postgres

volumes:
  postgres_data:

// What im running

npm run docker:up

// Error im having

> RUN npm run prisma:deploy:
> [email protected] prisma:deploy
> npx prisma migrate deploy
> Environment variables loaded from .env
> Prisma schema loaded from prisma/schema.prisma
> Datasource "db": PostgreSQL database "personal", schema "public" at "localhost:5432"
> Error: P1001: Can't reach database server at localhost:5432
> Please make sure your database server is running at localhost:5432.

// .env

# DATABASE_URL="postgresql://postgres:pass123@postgres:5432/personal?schema=public"
DATABASE_URL="postgresql://postgres:pass123@localhost:5432/personal?schema=public"
PORT=8080
HOST="0.0.0.0"

Obs: if i run docker:up only for postgres with DATABASE_URL="postgresql://postgres:pass123@localhost:5432/personal?schema=public" (on localhost) i can access the postgress normally with DBeaver

I tried to run change DATABASE_URL to:
DATABASE_URL="postgresql://postgres:pass123@postgres:5432/personal?schema=public"
and
DATABASE_URL="postgresql://postgres:pass123@localhost:5432/personal?schema=public"

2

Answers


  1. Chosen as BEST ANSWER

    I've solved the problem with this steps: (I'm still accepting other more practical answers to solve this problem)

    RUN -> docker container ls

    COPY: name of the postgres container

    RUN -> docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' CONTAINER_NAME

    COPY: id of the postgres container

    CHANGE:

    DATABASE_URL="postgresql://postgres:pass123@localhost:5432/personal?schema=public" (localhost:5432)

    to

    DATABASE_URL="postgresql://postgres:[email protected]:5432/personal?schema=public" (CONTAINER_IP:5432)

    Obs: I've tried to set DATABASE_URL="postgresql://postgres:pass123@postgres:5432/personal?schema=public" (SERVICE_NAME_ON_DOCKER_COMPOSE:5432) but doens't worked


  2. When you run your Postgres database in a Docker container and then try to access it from another service, like your Node.js application also running in a Docker container, localhost will not work as expected.

    In the context of Docker, localhost refers to the current container. So when your Prisma client tries to connect to localhost:5432, it’s looking for a PostgreSQL server inside the same container as your Node.js app, which isn’t there.

    To fix this issue:

    1. Use the service name defined in docker-compose.yml as the hostname for your database URL
    2. Ensure that environment variables are correctly set up for use within your Docker containers
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search