我能够构建docker映像,但无法运行容器。这是包.json:
{
"name": "linked-versions-viewer",
"version": "1.0.0",
"description": "...",
"main": "./dist/bundle.js",
"type": "module",
"scripts": {
"test": "echo "Error: no test specified" && exit 1",
"clean": "rimraf dist",
"build_only": "rollup -c",
"build": "cross-env NODE_ENV=production npm run clean && npm run build_only",
"serve": "parcel src/index.html --port 12345",
"start": "npm run clean && npm run serve"
},
"repository": {
"type": "git",
"url": "..."
},
"author": "...",
"license": "...",
"dependencies": {
"antd": "^4.12.3",
"base-widget": "...",
"react": "^17.0.2",
"react-dom": "^17.0.1"
},
"devDependencies": {
"@types/node": "^14.14.28",
"@types/react": "^17.0.2",
"@types/react-dom": "^17.0.1",
"cross-env": "^7.0.3",
"parcel-bundler": "^1.12.4",
"rev-hash": "^3.0.0",
"rimraf": "^3.0.2",
"rollup": "^2.39.0",
"rollup-plugin-commonjs": "^10.1.0",
"rollup-plugin-node-resolve": "^5.2.0",
"rollup-plugin-replace": "^2.2.0",
"rollup-plugin-terser": "^7.0.2",
"rollup-plugin-typescript2": "^0.29.0",
"rollup-plugin-uglify": "^6.0.4",
"typescript": "^4.1.5"
}
}
这是码头文件:
FROM node:14.17.0 as base
ENV NODE_ENV=production
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 8080
FROM base as production
ENV NODE_PATH=./build
CMD ["npm", "start"]
我通过运行docker build -t testapp .
创建了映像,并尝试使用docker run -p 8080:8080 -d testapp
运行容器,但它一直立即退出,代码为1。这是图像的错误日志:
> linked-versions-viewer@1.0.0 start /app
> npm run clean && npm run serve
> linked-versions-viewer@1.0.0 clean /app
> rimraf dist
sh: 1: rimraf: not found
npm ERR! code ELIFECYCLE
npm ERR! syscall spawn
npm ERR! file sh
npm ERR! errno ENOENT
npm ERR! linked-versions-viewer@1.0.0 clean: `rimraf dist`
npm ERR! spawn ENOENT
npm ERR!
npm ERR! Failed at the linked-versions-viewer@1.0.0 clean script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2021-06-09T20_40_17_204Z-debug.log
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! linked-versions-viewer@1.0.0 start: `npm run clean && npm run serve`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the linked-versions-viewer@1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
我试着完全删除rimraf依赖项,但后来在包裹依赖项上出现了类似的错误,所以可能软件包安装不正确?如果有帮助的话,这个应用程序也是用字体编写的。
不知道下一步该做什么。谢谢你的建议。
表单npm install
文档,
使用
--production
标志(或者当NODE_ENV
环境变量设置为production
时(,npm将不会安装devDependencies
中列出的模块
由于base
映像中有ENV NODE_ENV=production
,因此rimraf
和parcel-bundler
都未安装在容器中。
您的npm start
命令正在运行npm run clean && npm run serve
。npm run clean
使用rimraf
模块,npm run serve
使用parcel-bundler
模块。这就是您看到这两个错误的原因。
您可以尝试以下解决方案之一,
-
从Dockerfile中删除
ENV NODE_ENV=production
(这是最快的解决方案,但不应在生产中使用( -
您可以使用在容器内全局安装
rimraf
和parcel-bundler
RUN npm install --global rimraf && npm install --global parcel-bundler
然而,我仍然不认为这是一个好的生产准备设置。
-
你可以在你的容器中使用
npm run build
正确地构建你的应用程序并提供它。然而,我对React的熟悉程度不够,无法帮助你在Docker上设置它。