简体   繁体   English

使用node:16.15.1(alpine 3.16)将python2添加到Docker?

[英]Adding python2 to Docker using node:16.15.1 (alpine 3.16)?

I need to include python2 in my Dockerfile for a Vue app in order to build a dependency node-sass@4.14.1 (defined in package.json).我需要在 Vue 应用程序的 Dockerfile 中包含 python2 以构建依赖项 node-sass@4.14.1(在 package.json 中定义)。 The following works fine:以下工作正常:

FROM node:16.15.0-alpine as builder 
RUN apk add --no-cache python2 make g++
...
ADD app/package.json .
RUN CXXFLAGS="--std=c++14" npm install

But this node image is using alpine 3.15, which has a critical vulnerability in zlib that I would like to remove, so I want to use a newer alpine version.但是这个节点映像使用的是 alpine 3.15,它在 zlib 中有一个我想删除的严重漏洞,所以我想使用更新的 alpine 版本。 But using eg node:16.15.1-alpine (which uses alpine-3.16) then apk add fails, since python2 is no longer included in that image.但是使用例如 node:16.15.1-alpine (使用 alpine-3.16)然后 apk add 失败,因为 python2 不再包含在该图像中。

I tried to set the PYTHON env variable and use python3 instead, but then the build of node-gyp@3.8.0 (used by node-sass@4.14.1) fails with SyntaxError: Missing parentheses in call to 'print', so for this version, python2 seems to be needed to build.我尝试设置 PYTHON env 变量并改用 python3,但随后构建 node-gyp@3.8.0(由 node-sass@4.14.1 使用)失败并出现 SyntaxError: Missing parentheses in call to 'print',所以对于这个版本,似乎需要 python2 来构建。

In package.json, my devDependencies are:在 package.json 中,我的 devDependencies 是:

"devDependencies": {
...
 "node-sass": "^4.14.1",
 "sass-loader": "^7.0.1"

(Yes, I realize that the node-sass version is old (as is python2), and also that node-sass is deprecated, but trying to use a newer version of this package lead to other issues, so I first wanted to try to just make the build work with existing versions, but a newer alpine.) (是的,我意识到 node-sass 版本很旧(就像 python2 一样),并且 node-sass 已被弃用,但尝试使用此 package 的较新版本会导致其他问题,所以我首先想尝试只需使用现有版本进行构建,但使用更新的 alpine。)

How can I add python2 when using node:16.15.1-alpine as base image?使用 node:16.15.1-alpine 作为基础镜像时如何添加 python2?

For future readers - this is not an answer on how to add python2, but an answer to how I resolved the root problem - the failing build:对于未来的读者 - 这不是关于如何添加 python2 的答案,而是关于我如何解决根本问题的答案 - 失败的构建:

The package that needed python2 to build was node-gyp, which was needed by node-sass.需要python2构建的package是node-gyp,是node-sass需要的。 To eliminate this need, I replaced node-sass with sass:为了消除这种需要,我用 sass 替换了 node-sass:

npm uninstall node-sass
npm install sass

As sass does not require neither python nor g++ to be built, I could simplify the Dockerfile to由于 sass 既不需要 python 也不需要 g++ 构建,我可以将 Z3254677A71217C6C5FBF586 简化为

FROM node:16.15.1-alpine as builder
...
ADD app/package.json .
RUN npm install
...

Now I can upgrade the node image to 16.15.1 and further, which is what I need.现在我可以将节点映像升级到 16.15.1 及更高版本,这正是我所需要的。

Please still feel free to answer how python2 could have been added, in case future readers might still need it.请随时回答如何添加 python2,以防将来的读者可能仍然需要它。 But in order to resolve the build issue, my solution was to change package.但为了解决构建问题,我的解决方案是更改 package。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM