1

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
6
  • Not too hot at docker, so feel free to blast me. I am just stuck in google purgatory atm Commented Apr 12, 2022 at 18:06
  • If you run your npx prisma migrate command as part of your build (I can't see it in the files you've posted), then Postgres isn't available at that time. Postgres is only available at run-time. Commented Apr 12, 2022 at 18:18
  • I am running it in the terminal after I have already started docker containers Commented Apr 12, 2022 at 22:48
  • I figured it out. Changing DATABASE_URL=postgres://postgres@postgres/webapp_dev to DATABASE_URL=postgres://postgres@localhost/webapp_dev in the .env file worked for me Commented Apr 13, 2022 at 18:10
  • 2
    My whole team are still having issues with this. Totally unable to get the the root cause of the problem. Sometimes it works, sometimes it doesn't. I would suggest that if you get it working, don't rebuild your db or docker images. This issue is pesky and intermittent. Commented Apr 15, 2022 at 15:00

2 Answers 2

5

I got the same error I solved it after adding ?connect_timeout=300 at my DATABASE_URL

Sign up to request clarification or add additional context in comments.

Comments

2

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

1 Comment

I just added "?schema=public" and it works fine. The database hosting did not include this one upon copy.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.