postgresqlnestjsprismacockroachdb

Error: P1001: Can't reach database server at `crdb`:`26257`


Framework:Nest js ORM: Prisma Database: Cockroach DB (like postgres) OS: Debian 12

I want to run a container using Docker Compose and am getting this error.

Prisma schema loaded from prisma/schema.prisma
Datasource "db": CockroachDB database "defaultdb", schema "public" at "crdb:26257"

Error: P1001: Can't reach database server at `crdb`:`26257`

Please make sure your database server is running at `crdb`:`26257`.

The command '/bin/sh -c npx prisma db push' returned a non-zero code: 1
ERROR: Service 'app' failed to build : Build failed

The problem is in the Database_URL, I'm sure because I ran this project before without Docker

ENV file: DATABASE_URL="postgresql://root@crdb:26257/defaultdb?sslmode=disable"

Docker-compose

version: '3.5'
services:
  crdb:
   container_name: crdb
   image: cockroachdb/cockroach:latest
   ports:
     - "26257:26257"
     - "8080:8080"
   command: start-single-node --insecure 
   volumes:
     - ./data/node_1:/cockroach/cockroach-data

  app:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: nest
    environment:
      - PORT=5000
    ports:
      - '3000:3000'
    depends_on:
      - crdb
    volumes:
      - ./src:/app/src

schema.prisma

generator client {
  provider = "prisma-client-js"
  previewFeatures = ["fullTextSearch", "fullTextIndex"] 
}

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

Dockerfile

###################
# BUILD FOR LOCAL DEVELOPMENT
###################

FROM node:18-alpine As development

WORKDIR /usr/src/app

# Copy application dependency manifests to the container image.
# A wildcard is used to ensure copying both package.json AND package-lock.json (when available).
# Copying this first prevents re-running npm install on every code change.
COPY --chown=node:node package*.json ./

RUN npm ci

COPY --chown=node:node . .

USER node

###################
# BUILD FOR PRODUCTION
###################

FROM node:18-alpine As build

WORKDIR /usr/src/app

COPY --chown=node:node package*.json ./

COPY --chown=node:node --from=development /usr/src/app/node_modules ./node_modules

COPY --chown=node:node . .

RUN npx prisma db push

RUN npx prisma generate

RUN npm run build

ENV NODE_ENV production

RUN npm ci --only=production && npm cache clean --force

USER node

###################
# PRODUCTION
###################

FROM node:18-alpine As production

COPY --chown=node:node --from=build /usr/src/app/node_modules ./node_modules
COPY --chown=node:node --from=build /usr/src/app/dist ./dist

CMD [ "node", "dist/main.js" ]

Without a description of the app service, DB starts

I tried to make the following URLs like this DATABASE_URL="postgresql://root@localhost:26257/defaultdb?sslmode=disable" DATABASE_URL="postgresql://root@127.0.0.1:26257/defaultdb?sslmode=disable" DATABASE_URL="postgresql://root@crdb1:26257/defaultdb?sslmode=disable"(crdb - service; crdb1 - container) Once I was able to find out the ip of a container using docker inspect and docker ps, but I’m not sure that it was the ip of that container (how would I know if it doesn’t start?).

So I'm thinking...maybe I need to define a network? Because when the database is initialized, the network name is displayed in the console(after docker-compose system prune). Or i cant connect to single-node? In a similar question, the person deletes the database from the OS and it helps... should I try?


Solution

  • I solve that!(probably)

    So i create a entrypoint.sh

    #!/bin/sh
    
    npx prisma db push
    
    # Run the main container command
    exec "$@"
    
    

    And add in Dockerfile ENTRYPOINT ["./entrypoint.sh"] instead off npx prisma db push. And it's works for me.

    Why it's works? - link in my 2nd comment