skip to Main Content

After hours of searchs, I must bow dow and ask you some advices on my problem :

My backend (express + prisma + postgresql) is Dockerized, functionning BUT I can’t use npx prisma commands from my wsl2 zsh terminal.

Here is my .env

# Database settings
NODE_ENV=dev
DB_USER=user
DB_PASS=password
DATABASE_URL="postgresql://${DB_USER}:${DB_PASS}@postgres/chimere?schema=public"

Dockerfile :

FROM node:17-alpine3.14 as base

WORKDIR /user/src/app
COPY package*.json /user/src/app/
EXPOSE 5000

FROM base as dev
ENV NODE_ENV=development
RUN npm install -g nodemon && npm install
COPY . /user/src/app/
RUN npx prisma generate
CMD ["nodemon", "src/index.js"]

FROM base as production
ENV NODE_ENV=production
RUN npm ci
COPY . /user/src/app/
RUN npx prisma generate
CMD ["node", "src/index.js"]

docker-compose.yml :

version: '3.8'
services:
  postgres:
    image: postgres
    restart: always
    environment:
      - POSTGRES_USER=${DB_USER}
      - POSTGRES_PASSWORD=${DB_PASS}
    volumes:
      - postgres:/var/lib/postgresql/data
    ports:
      - '5432:5432'
  web:
      build:
        context: ./
        target: dev
      restart: always
      volumes:
        - .:/usr/src/app
        - uploaded-files:/usr/src/app/public/media/files
        - uploaded-pictures:/usr/src/app/public/media/pictures
      command: npm run start:dev
      ports:
        - "5000:5000"
      environment:
        NODE_ENV: development
        DEBUG: nodejs-docker-express:*

volumes:
    postgres:
    uploaded-files:
    uploaded-pictures:

and Prisma Schema :

generator client {
    provider      = "prisma-client-js"
    binaryTargets = ["native", "linux-musl"]
}

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

Like you can see I’m prettry new to Docker and almost everything is an adjusted copypasta from Google (:

How can I get my app to work AND get my commands to work aswell ?

Thanks !

2

Answers


  1. I was facing the same issue but it was on a mysql container. Basically I was having a problem with my DATABASE_URL in my .env file. It was looking something like this:

    DATABASE_URL="mysql://${DB_USER}:${DB_PASS}@localhost:3306/project_name"
    

    The problem was that localhost. Apparently, when running inside a container, instead of the localhost, it uses the container’s name. I changed my docker compose to specify the container’s name:

    version: '3.1'
    
    services:
    
      db:
        image: mysql
        container_name: mysql
        ports:
          - 3306:3306
    

    Notice the container_name property. After that, I changed my .env to:

    DATABASE_URL="mysql://${DB_USER}:${DB_PASS}@mysql:3306/project_name"
    

    I would suggest you to try something similar. Maybe something along these lines:

    version: '3.8'
    services:
      postgres:
        image: postgres
        container_name: postgres
        restart: always
        environment:
          - POSTGRES_USER=${DB_USER}
          - POSTGRES_PASSWORD=${DB_PASS}
        volumes:
          - postgres:/var/lib/postgresql/data
        ports:
          - '5432:5432'
    

    And for your .env you can leave the way it is now unless you chose a different container’s name, then you would subsitute with the name you chose inside the curly braces (remember to remove the curly braces):

    DATABASE_URL="postgresql://${DB_USER}:${DB_PASS}@{container_name}/chimere?schema=public"
    

    Check this GitHub issue for more information as well:
    https://github.com/prisma/prisma/issues/1385

    Login or Signup to reply.
  2. You need to act from inside the container.

    First, create an interactive shell in the container using docker exec:

    docker exec -it <name of your containter> sh
    

    Note: the -i flag keeps input open to the container, and the -t flag creates a pseudo-terminal that the shell can attach to.

    Then, once inside the container, execute the commands you need:

    npx prisma migrate dev --name <name of your migration>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search