Docker Build未生效package.json中semver版本override问题求助
问题描述
近期发现semver npm包存在漏洞,我已在package.json中添加overrides字段指定semver版本为^7.5.3。本地执行npm ci后,node_modules中仅安装了semver 7.5.3,npm audit返回0漏洞。
我的Dockerfile内容如下:
FROM docker.io/library/node:16.20.0-alpine AS build WORKDIR /build COPY package.json package-lock.json .npmrc ./ RUN npm ci COPY . ./ RUN npm run-script build FROM docker.io/library/node:16.20.0-alpine WORKDIR /app COPY --from=build /build/build public/ RUN npm i -g http-server ENTRYPOINT [ "http-server" ]
但执行docker build后,在镜像的/usr/local/lib/node_modules/npm/node_modules/semver/package.json中发现安装的是semver 7.3.7。本地和镜像均运行npm 8.19.4,我已复制本地的package.json和package-lock.json并执行npm ci,本地能得到正确版本,但镜像中不行。
我的package.json内容如下:
{ "name": "mats-frontend-web", "version": "1.0.0", "description": "Web Frontend UI", "dependencies": { "@essnextgen/ui-kit": "^0.7.3-alpha.55", "@microsoft/applicationinsights-react-js": "^3.3.6", "@microsoft/applicationinsights-web": "^2.8.5", "@testing-library/jest-dom": "^5.16.4", "@testing-library/react": "^11.2.7", "@testing-library/react-hooks": "^7.0.2", "@testing-library/user-event": "^12.8.3", "@types/react-dom": "^17.0.17", "axios": "^0.27.2", "crypto-js": "^4.1.1", "history": "^5.0.1", "jest-when": "^3.5.1", "js-cookie": "^3.0.1", "powerbi-client": "^2.21.1", "react": "^17.0.2", "react-device-detect": "^1.17.0", "react-dom": "^17.0.2", "react-error-boundary": "^3.1.4", "react-ga4": "^1.4.1", "react-gtm-module": "^2.0.11", "react-router-dom": "^5.3.3", "typescript": "^4.7.4", "web-vitals": "^2.1.4" }, "overrides": { "semver": "^7.5.3" }, "scripts": { "start": "react-scripts start", "build": "react-scripts build", "test": "react-scripts test", "eject": "react-scripts eject", "prepare": "husky install", "test:ci": "CI=true react-scripts test --testResultsProcessor ./node_modules/jest-junit" }, "repository": { "type": "git", "url": "git@ssh.dev.azure.com:v3/frontend-web" }, "browserslist": { "production": [ ">0.2%", "not dead", "not op_mini all" ], "development": [ "last 1 chrome version", "last 1 firefox version", "last 1 safari version" ] }, "devDependencies": { "@types/crypto-js": "^4.1.1", "@types/jest": "^26.0.24", "@types/jest-when": "^3.5.2", "@types/js-cookie": "^2.2.7", "@types/mocha": "^9.1.1", "@types/node": "^12.20.55", "@types/react": "^17.0.48", "@types/react-gtm-module": "^2.0.1", "@types/react-router-dom": "^5.3.0", "@typescript-eslint/eslint-plugin": "^4.31.1", "@typescript-eslint/parser": "^4.31.1", "eslint": "^7.32.0", "eslint-config-prettier": "^8.3.0", "eslint-import-resolver-typescript": "^2.7.1", "eslint-plugin-flowtype": "^6.0.1", "eslint-plugin-import": "^2.26.0", "eslint-plugin-jest": "^24.4.2", "eslint-plugin-jest-dom": "^4.0.2", "eslint-plugin-jsx-a11y": "^6.6.1", "eslint-plugin-react": "^7.30.1", "eslint-plugin-react-hooks": "^4.6.0", "eslint-plugin-testing-library": "^4.12.3", "husky": "^8.0.3", "jest-junit": "^13.2.0", "lint-staged": "^13.2.1", "msw": "^0.44.2", "node-talisman": "^1.29.10", "prettier": "^2.7.1", "react-scripts": "^5.0.0", "react-test-renderer": "^17.0.2" } }
问题原因
你看到的/usr/local/lib/node_modules/npm/node_modules/semver是全局npm自身的依赖包,和你的项目依赖无关。你的项目指定的semver版本在build阶段的/build/node_modules目录中,但最终镜像并没有复制这部分内容——你只把build产物复制到了最终镜像,最终镜像里的npm是基础镜像自带的,它的semver版本由基础镜像的npm版本决定,不受你项目中overrides字段的影响。
解决办法
- 升级Node.js基础镜像:选择包含更高版本npm的node镜像,比如
node:16.20.1-alpine(npm版本更高,自带的semver也会更新),或者直接升级到node:18-alpine系列,其自带的npm版本会包含修复漏洞的semver版本。 - 改用静态文件服务器镜像:如果你的最终镜像只需要托管静态build产物,完全可以用nginx这类轻量镜像替代Node环境,彻底避开npm相关的依赖问题。
- 手动更新npm的semver依赖:在最终镜像中执行命令替换npm自带的semver版本:
这条命令会先全局安装指定版本的semver,再进入npm的目录安装指定版本覆盖原有依赖。RUN npm install -g semver@^7.5.3 && npm explore npm -- npm install semver@^7.5.3
内容的提问来源于stack exchange,提问作者user17203430
相关产品推荐
相关产品推荐

