我的nodeJS后端有问题。我想创建一个docker编写环境,但是当我尝试启动我的Prisma NodeJS应用程序时,我一直收到这个错误。我已经尝试了很多方法,但我在Google上找不到任何可用的解决方案。该应用程序在没有docker的情况下工作。
Docker合成文件:
endbit-express:
container_name: endbit-express
build: ./endbit-express
volumes:
- ./endbit-express:/app
- /app/node_modules
ports:
- 8080:8080
depends_on:
- mysql
environment:
- DATABASE_URL=mysql://root:root@localhost:3306/endbit
networks:
- endbit
mysql:
container_name: endbit-mysql
image: mysql:8.0.28
restart: always
ports:
- 6033:3306
environment:
- MYSQL_DATABASE=endbit
- MYSQL_ROOT_PASSWORD=root
volumes:
- dbdata:/var/lib/mysql
networks:
- endbit
Docker文件:
FROM node:17.4.0
WORKDIR /app
COPY package*.json ./
COPY prisma ./prisma
RUN npm install
COPY . .
RUN prisma generate
EXPOSE 8080
CMD ["npm", "start"]
错误:
endbit-express | > express@0.0.0 start
endbit-express | > node ./bin/www
endbit-express |
endbit-express | /app/node_modules/.prisma/client/index.js:3
endbit-express | throw new Error(
endbit-express | ^
endbit-express |
endbit-express | Error: @prisma/client did not initialize yet. Please run "prisma generate" and try to import it again.
endbit-express | In case this error is unexpected for you, please report it in https://github.com/prisma/prisma/issues
endbit-express | at new PrismaClient (/app/node_modules/.prisma/client/index.js:3:11)
endbit-express | at Object.<anonymous> (/app/config/passport.js:7:16)
endbit-express | at Module._compile (node:internal/modules/cjs/loader:1097:14)
endbit-express | at Object.Module._extensions..js (node:internal/modules/cjs/loader:1149:10)
endbit-express | at Module.load (node:internal/modules/cjs/loader:975:32)
endbit-express | at Function.Module._load (node:internal/modules/cjs/loader:822:12)
endbit-express | at Module.require (node:internal/modules/cjs/loader:999:19)
endbit-express | at require (node:internal/modules/cjs/helpers:102:18)
endbit-express | at Object.<anonymous> (/app/app.js:7:1)
endbit-express | at Module._compile (node:internal/modules/cjs/loader:1097:14)
endbit-express |
endbit-express | Node.js v17.4.0
2条答案
按热度按时间k5hmc34c1#
在
RUN prisma generate
之前添加RUN npm i -g prisma
,它应该可以工作tpgth1q72#
最后我自己找到了答案,因为我使用的是一个docker compose文件,docker不能识别localhost:3306 URL。在docker compose中,你需要通过容器的名称来链接容器。所以我用下面的代码(host = docker service name)修改了后端组件环境: