0

I have a project setup using Docker-compose , Node , prisma and postgres. here is the docker compose file

version: '3.8'
services:
  # These are the configurations for our Node app
  # When Docker Compose starts this container it will automatically
  # use the Dockerfile in the directory to configure it
  app:
    build: .
    depends_on:
      # Our app does not work without our database
      # so this ensures our database is loaded first
      - postgres
    ports:
      - "8080:8080"
    volumes:
      # Maps our current project directory `.` to
      # our working directory in the container
      - ./:/usr/src/app/
      # node_modules workaround for volumes
      # https://stackoverflow.com/a/32785014
      - /usr/src/app/node_modules

  # This is the configuration for our PostgreSQL database container
  # Note the `postgres` name is important, in out Node app when we refer
  # to  `host: "postgres"` that value is mapped on the network to the 
  # address of this container.
  postgres:
    image: postgres:14.1-alpine
    restart: always
    environment:
      # You can set the value of environment variables
      # in your docker-compose.yml file
      # Our Node app will use these to connect
      # to the database
      - POSTGRES_USER=root
      - POSTGRES_PASSWORD=password
      - POSTGRES_DB=ecom-poc
    ports:
      # Standard port for PostgreSQL databases
      - "5432:5432"
    volumes:
      # When the PostgresSQL container is started it will run any scripts
      # provided in the `docker-entrypoint-initdb.d` directory, this connects
      # our seed file to that directory so that it gets run
      - ./database-seed.sql:/docker-entrypoint-initdb.d/database-seed.sql
  adminer: 
    image: adminer 
    links: 
      - postgres:postgres 
    ports: 
      - 3001:8080
    depends_on:
      - postgres

Dockerfile

FROM node:18-slim

# Create app directory
WORKDIR /usr/src/app

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
# copying packages first helps take advantage of docker layers
COPY package*.json ./

RUN npm install
# If you are building your code for production
# RUN npm ci --only=production

# Bundle app source
COPY . .

EXPOSE 8080

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

.env

DATABASE_URL="postgresql://root:password@postgres:5432/ecom-poc"

as you can see above the container name for Postgres is postgres when I try running prisma commands like npx prisma migrate I would get an error

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

I searched online and changed my connection string to localhost

.env

DATABASE_URL="postgresql://root:password@localhost:5432/ecom-poc"

now the command line did end up working after this but problem arises when I try to work with prisma-client here is my index.ts file

index.ts

import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient() 

const express = require("express");
const app = express();
const port = 8080;

app.use(express.static("public"));

app.get("/", async (req:any, res:any) => {
  res.setHeader("Content-Type", "application/json");
  res.status(200);
  res.send("wallah!")
});

app.listen(port, async () => {
  console.log(`Example app listening at http://localhost:${port}`);
  console.log(await prisma.$connect())
});

now I start getting

Please make sure your database server is running at `localhost`:`5432`.

again!,now switching it back to

.env

DATABASE_URL="postgresql://root:password@postgres:5432/ecom-poc"

will work for code now, but not for command line!

I have configured Adminer and it does work with host postgres

This behaviour is a little confusing for me why doesnt prisma accept one host?

user3692459
  • 132
  • 10
  • It's not totally obvious but I suspect your `npx ....` command is ran directly on your machine. A compose service name is only known between the members of a same network in docker-compose. Your docker host is not part of that and will not resolve the name `postgres`. It works with localhost because you forwarded the postgres port to your docker host (`ports: ['5342:5342']`). It will work with `postgres` if you run that command from your app container for example, something like `docker compose run app npx prisma migrate` if your image has the necessary tools installed. – Zeitounator May 25 '23 at 15:49
  • @Zeitounator you're right! it does work when I run command in the container, is there any convenient way so that I don't have to login to the container and run commands? how can we do it on host machine? – user3692459 May 27 '23 at 22:07
  • From the host machine, you have to target localhost. – Zeitounator May 28 '23 at 05:25

0 Answers0