Knuth
Knuth

Reputation: 151

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 [email protected] (defined in 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. But using e.g. node:16.15.1-alpine (which uses alpine-3.16) then apk add fails, since python2 is no longer included in that image.

I tried to set the PYTHON env variable and use python3 instead, but then the build of [email protected] (used by [email protected]) fails with SyntaxError: Missing parentheses in call to 'print', so for this version, python2 seems to be needed to build.

In package.json, my devDependencies are:

"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.)

How can I add python2 when using node:16.15.1-alpine as base image?

Upvotes: 3

Views: 4115

Answers (2)

Khrystyna
Khrystyna

Reputation: 81

To install python2 with latest Alpine just add repo which still has python2 in it. Like:

RUN echo "https://dl-cdn.alpinelinux.org/alpine/v3.15/community"  >> /etc/apk/repositories
RUN apk add python2 make g++ && rm -rf /var/cache/apk/*

Works perfectly for Alpine 3.16

Upvotes: 8

Knuth
Knuth

Reputation: 151

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:

The package that needed python2 to build was node-gyp, which was needed by node-sass. To eliminate this need, I replaced node-sass with 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

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.

Please still feel free to answer how python2 could have been added, in case future readers might still need it. But in order to resolve the build issue, my solution was to change package.

Upvotes: 11

Related Questions