这是我的Node应用程序的package.json
:
{
"name": "my-graphql-app",
"version": "1.0.0",
"description": "My Node GraphQL APP",
"private": true,
"license": "UNLICENSED",
"scripts": {
"start": "npm run run:watch",
"build": "webpack",
"build:watch": "webpack --watch",
"run": "node ./dist/app.js",
"run:watch": "nodemon ./dist/app.js"
},
"dependencies": {
"@babel/core": "^7.8.4",
"@babel/polyfill": "^7.8.3",
"@babel/preset-env": "^7.8.4",
"apollo-server-express": "^2.10.1",
"babel-loader": "^8.0.6",
"body-parser": "^1.19.0",
"cors": "^2.8.5",
"express": "^4.17.1",
"graphql": "^14.6.0",
"mysql2": "^2.1.0",
"nodemon": "^2.0.2",
"npm-run-all": "^4.1.5",
"sequelize": "^5.21.5",
"webpack-cli": "^3.3.11"
},
"devDependencies": {
"webpack": "^4.41.6",
"webpack-node-externals": "^1.7.2"
}
}
当我在本地计算机上运行npm install
和npm start
时,此方法工作正常(以防万一,请在Mac OS Catalina 10.15.3上运行它)。我的本地节点版本为v11.10.1
。
我要在Docker容器中使相同的Node应用程序正常工作真是疯狂。我做了许多不同的尝试,分别调整了package.json
和Dockerfile
,但是我总是遇到一些npm install错误或import错误。
我正在尝试使用docker-compose up --build
启动容器(我也有一个MySQL容器)。
这是我的Dockerfile
:
FROM node:12
EXPOSE 5000
WORKDIR /graphql_api
COPY package.json .
RUN npm install
COPY . /graphql_api
CMD npm start
这是我的docker-compose.yml
:
version: '3'
services:
database:
image: mysql:5.7
environment:
- 'MYSQL_ROOT_PASSWORD=pwd'
- 'MYSQL_DATABASE=testdb'
- 'MYSQL_USER=user'
- 'MYSQL_PASSWORD=pwd'
volumes:
- ./db:/docker-entrypoint-initdb.d
restart: always
ports:
- 8006:3306
graphql_api:
build: ./graphql_api
container_name: graphql_api
restart: always
volumes:
- ./graphql_api:/graphql_api
- /graphql_api/node_modules
ports:
- "5000:5000"
depends_on:
- database
(顺便说一下,MySQL容器可以很好地启动,并且数据库本身已使用我在init.sql
文件中定义的所有表正确初始化了。)
一旦构建过程达到RUN npm install
点,我总是会收到以下警告:
npm WARN deprecated core-js@2.6.11: core-js@<3 is no longer maintained and not recommended for usage due to the number of issues. Please, upgrade your dependencies to the actual version of core-js@3.
然后我收到一堆这样的消息
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@~2.1.2 (node_modules/chokidar/node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@2.1.2: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@^1.2.7 (node_modules/watchpack/node_modules/chokidar/node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.11: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: abbrev@1.1.1 (node_modules/watchpack/node_modules/fsevents/node_modules/abbrev):
npm WARN enoent SKIPPING OPTIONAL DEPENDENCY: ENOENT: no such file or directory, rename '/graphql_api/node_modules/watchpack/node_modules/fsevents/node_modules/abbrev' -> '/graphql_api/node_modules/watchpack/node_modules/fsevents/node_modules/.abbrev.DELETE'
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: ansi-regex@2.1.1 (node_modules/watchpack/node_modules/fsevents/node_modules/ansi-regex):
(我没有全部复制粘贴,因为确实有很多 。)
收到这些消息之后
added 785 packages from 486 contributors and audited 8930 packages in 26.85s
这使我认为npm install
确实起到了一定作用,尽管有先前的消息。
但是,当构建过程达到CMD npm start
时,会发生这种情况:
graphql_api | > my-graphql-app@1.0.0 start /graphql_api
graphql_api | > npm-run-all --parallel build:watch run:watch
graphql_api |
graphql_api | sh: 1: npm-run-all: not found
graphql_api | npm ERR! code ELIFECYCLE
graphql_api | npm ERR! syscall spawn
graphql_api | npm ERR! file sh
graphql_api | npm ERR! errno ENOENT
graphql_api | npm ERR! my-graphql-app@1.0.0 start: `npm-run-all --parallel build:watch run:watch`
graphql_api | npm ERR! spawn ENOENT
graphql_api | npm ERR!
graphql_api | npm ERR! Failed at the my-graphql-app@1.0.0 start script.
graphql_api | npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
我还尝试从node:11
映像而不是node:12
映像开始构建容器,以匹配我在本地安装的Node版本,但是结果是相同的。
我真的很茫然:在谷歌搜索和查看其他StackOverflow答案之后,我尝试过改变很多事情(例如,在"private": true
中添加pacakge.json
的原因是我发现了一个答案这里有一个类似的问题),但我仍然无法运行此容器。
答案 0 :(得分:2)
根据您的错误消息,缺少软件包npm-run-all。在dockerfile中,在“ npm install”行之前(全局)安装此软件包,然后重试。
希望有帮助!
答案 1 :(得分:0)
您可以尝试将./node_modules/.bin/
添加到dockerfile中的PATH中。这可能是npm-run-all所在的地方。
答案 2 :(得分:0)
如果您从./graphql_api/node_modules/.bin/执行命令,那么npm-run-all将会起作用。
然后在这里您将有两个选择。
ENV PATH="/graphql_api/node_modules/.bin/:${PATH}"
npm i -g npm-run-all
以全局方式安装npm-run-all。 在查看下面引用的问题时,请参阅我发现的此特定问题。 https://github.com/mysticatea/npm-run-all/issues/96
答案 3 :(得分:0)
唯一的方法是将/graphql_api/node_modules/.bin/导出到PATH。位置可以防止这些影响