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

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


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source