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
I was facing the same issue but it was on a
mysql
container. Basically I was having a problem with myDATABASE_URL
in my.env
file. It was looking something like this:The problem was that
localhost
. Apparently, when running inside a container, instead of thelocalhost
, it uses the container’s name. I changed my docker compose to specify the container’s name:Notice the
container_name
property. After that, I changed my.env
to:I would suggest you to try something similar. Maybe something along these lines:
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):Check this GitHub issue for more information as well:
https://github.com/prisma/prisma/issues/1385
You need to act from inside the container.
First, create an interactive shell in the container using docker exec:
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: