skip to Main Content

Trying to dockerize, nests, and Prisma.
Nest is responding correctly to curl requests and and I can connect to the Postgres server fine with this command

— docker compose exec postgres psql -h localhost -U postgres -d webapp_dev

Everything works until i try to run

npx prisma migrate dev --name init 

then i get back

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

Here is my code:

docker-compose.yml

version: "2"
services:
  backend:
    build: .
    ports:
      - 3000:3000
      - 9229:9229 # debugger port
    volumes:
      - .:/usr/src/app
      - /usr/src/app/node_modules
    command: yarn start:debug
    restart: unless-stopped
    depends_on:
      - postgres
    environment:
      DATABASE_URL: postgres://postgres@postgres/webapp_dev
      PORT: 8000
  postgres:
    image: postgres:14-alpine
    ports:
      - 5432:5432
    environment:
      POSTGRES_DB: webapp_dev
      POSTGRES_HOST_AUTH_METHOD: trust

DockerFile

FROM node:16

# Create app directory, this is in our container
WORKDIR /usr/src/app

# Install app dependencies
# Need to copy both package and lock to work
COPY package.json yarn.lock ./
RUN yarn install

COPY prisma/schema.prisma ./prisma/
RUN npx prisma generate

# Bundle app source
COPY . .


RUN yarn build

EXPOSE 8080
CMD ["node": "dist/main"]

.env

//.env 
DATABASE_URL=postgres://postgres@postgres/webapp_dev

2

Answers


  1. not sure if this is the only issue but your db url does not contain the db secret in it

    DATABASE_URL: postgres://postgres:mysecret@postgres/webapp_dev?schema=public
    
    Login or Signup to reply.
  2. I got the same error I solved it after adding ?connect_timeout=300 at my DATABASE_URL

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search