New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
node:7.9-alpine unable to build package due python is not installed #384
Comments
The point of the alpine image is to be as small as possible so it doesn't include python. See #282 on how to do it. |
@LaurentGoderre thanks for the help! I thought it was a problem in the |
Note that a new feature is coming to docker, the ability to have different build and run image which would solve this issue. |
I believe this is moby/moby#7115 for anyone else who was looking for it. |
@rwillians I have solved building native modules on alpine by adding the following to the
This way the image size is not affected because it's all in one layer. Thanks @LaurentGoderre will look out for that! |
Thanks for sharing @JamesKyburz |
This can be closed, right? We could probably put some caveats in the readme about using the alpine image, as well as some examples for using multi-stage builds. |
@SimenB can be closed. I think that would be of a great help to docker/node newcomers, like me |
My container still cannot find python2 in PATH even though I added the command mentioned above |
If you are using the given snippet above, you should notice that python is
been installed as a virtual dependency (which is a feature of alpine's APK)
and then it's been removed right after `npm install` execution.
So in order to whatever command you are trying to run work, it should run
before the "apk del native-deps" part.
|
@rwillians Thanks you so much! |
While that is true, it is also important to consider that it is |
I disagree that we should add anything to the alpine image other than node, npm and yarn. But we should document that fact so it's obvious to users. We have some text about this fact in the readme.
https://github.com/nodejs/docker-node/blob/master/README.md#nodealpine Maybe just mention python and/or native dependencies as well? PR welcome! |
I am struggling to see how having both |
I also see that you just mentioned |
Ah, that makes sense. Thanks for the clarification. |
I fixed the issue by adding these lines to my docker file
|
@Macmee I suggest you to use JamesKyburz's solution #384 (comment) in order to keep your image minimal. |
In case it helps anyone, I had to install a bunch of other RUN apk --no-cache add --virtual native-deps \
g++ gcc libgcc libstdc++ linux-headers autoconf automake make nasm python git && \
npm install --quiet node-gyp -g
WORKDIR /usr/src/app
COPY package*.json bower.json .bowerrc ./
RUN npm set progress=false && \
npm i --silent && \
$(npm bin)/bower --allow-root i
RUN apk del native-deps |
Worked ! Thanks |
Nailed it, thanks |
I recommend #384 (comment) to keep your image size smaller. |
This means that building the docker image now takes a long time (downloading, building g++, make, python). And --no-cache is required to keep the bundle size down is there any good way around this? Does anyone else see this as an issue? |
@tonypee Yes this seems to be a common issue, every time package.json is changed all build dependencies need to be installed again. This is because it is often on the same docker layer. I got around this issue using multi stage builds. Here is an example The dependencies aren't in the same layer as package.json avoiding this issue. Also because it uses multi stage builds the size of the image isn't affected :) FROM jameskyburz/node:10.15.3-alpine-devtools as devtools
LABEL maintainer="James Kyburz james.kyburz@gmail.com"
WORKDIR /usr/src/app
ENV NPM_CONFIG_LOGLEVEL warn
ARG NODE_ENV=development
COPY package.json package-lock*.json npm-shrinkwrap*.json /usr/src/app/
RUN \
npm i && \
npm rebuild bcrypt --build-from-source
FROM node:10.15.3-alpine
WORKDIR /usr/src/app
COPY . /usr/src/app
COPY --from=devtools /usr/src/app/node_modules /usr/src/app/node_modules
USER node
ENTRYPOINT ["node", "src/index"]
CMD []
EXPOSE 5000
|
runs before the time i need python |
The latest The reason I use this as a build step instead of installing python prior to 1.) The Docker image will be smaller ᐅ docker run -ti --rm jameskyburz/node:10.18.0-alpine3.11-devtools sh
/ # node-gyp --version
v5.0.7
/ # python3 --version
Python 3.8.1 ᐅ docker run -ti --rm jameskyburz/node:12.14.0-alpine3.11-devtools sh
/ # node-gyp --version
v5.0.7
/ # python3 --version
Python 3.8.1 |
I got stuck here using |
@zipzit same here, i'm using Not working:
And this earlier solution is not working:
|
I'm working in Node, React... my dockerfile:
I only tried the python2, python3 thing in desperation, but it seems to work just fine. And I'm not running from command line, instead I'm running from an integrated docker-compose.yml file. Note, this is still a work in progress. There may be errors below.. The dockerfile above is located within github.com/zipzit/privateRepoName. And I'm using the same container image for both frontend and backend servers.
|
Hey @zacharytyhacz, you gotta install FROM node:15.1.0-alpine3.12
RUN apk --no-cache add --virtual native-deps \
g++ gcc libgcc libstdc++ linux-headers make python2 && \
npm install --quiet node-gyp -g
COPY package.json ./
COPY package-lock.json ./
RUN npm ci && \
apk del native-deps Btw I'm not sure if all those deps are required anymore |
works fine |
|
@jmcbri I'm guessing in your case you need take this snippet #384 (comment) and replace FROM node:15.1.0-alpine3.12
RUN apk --no-cache add --virtual native-deps \
g++ gcc libgcc libstdc++ linux-headers make python3 && \
npm install --quiet node-gyp -g
COPY package.json ./
COPY package-lock.json ./
RUN npm ci && \
apk del native-deps |
Thanks. I'll give it a try. Have a great [time appropriate greeting]! |
Thanks @Macmee . This helped me a lot. |
Hey @malavshah9 👋 |
@rwillians Yes, I will do that also. Thanks for pointing out. |
There is a recipe for node-gyp alpine in this repo, that works just fine: FROM node:alpine
RUN apk add --no-cache --virtual .gyp python make g++ \
&& npm install \
&& apk del .gyp |
Nice @piotr-cz, we didn't have that back when the issue was created. I do recommend though to copy your package.json and/or package-lock.json file after you install the dependencies so that the dependencies cache layer don't get invalidated whenever your package files change. You'll get one extra layer for doing that (assuming you're not using multi-stage builds) but that's a tradeoff I'm often taking in favor of reduceing my build time. |
@rwillians My full Dockerfile is a mix of above recipe and Layer Caching from docker/getting-started repo: FROM node:14-alpine
RUN apk add --no-cache --virtual .gyp python make g++
ENV NODE_ENV=production
WORKDIR /home/node/app
COPY package.json package-lock.json ./
RUN npm install && \
apk del .gyp
COPY . .
EXPOSE 3000
CMD ["node", "src/index.js"]
I guess best option is to avoid dependencies which require node-gyp when possible. |
when iam using this got an error like this. |
Hey @Sundar21, the package |
If you arrived at the issue, it probably means you’re new at using Docker and/or Node.js, like I was when I opened this issue 5 years ago. And that’s alright, I’ll try making it easier for you :) We use So what I mean by “cost” is that, in order to have a smaller image, you gotta remove unnecessary stuff from it — or better yet, never add the unnecessary stuff in the first place —. For example, Naturally, you’ll need to install those missing dependencies by yourself and once you don’t need them anymore, you gotta delete them. That’s the approach you’ll see on snippets throughout theses comments below. The most recent snippet is right below, just know that it might not fit your specific needs. It might be installing more stuff than you need or it might not be installing stuff that you actually need, in which case you’ll need to figure out the missing dependencies on your own by going through the logs. Of course you can ask for help here, me and a lot of the people participating in this discussion might be able to help you. FROM node:17.8.0-alpine
WORKDIR /app
RUN apk --no-cache add --virtual virtual-deps \
g++ gcc libgcc libstdc++ linux-headers make python2 && \
npm install --quiet node-gyp -g
COPY package.json ./
COPY package-lock.json ./
RUN npm ci && \
apk del virtual-deps
COPY src/ src/
EXPOSE 3000
CMD ["npm", "start"] If you’re new to Docker, you should know that there’s another approach to address this issue using what’s called Multi-Stage Builds. This approach, applied to this issue, consists of using a base image (such as thee regular FROM node:17.8.0 AS build-stage
WORKDIR /app
COPY package.json package.json
COPY package-lock.json package-lock.json
RUN npm ci
FROM node:17.8.0-alpine
WORKDIR /app
COPY --from=build-stage /app ./
COPY src/ src/
EXPOSE 3000
CMD ["npm", "start"] I’m not in a good position to tell you which approach is better for you. For example, the second approach might take longer to build if you don't have cache but might be easier to work with while the first might be faster to build but it’s harder if you don’t know your way around finding the dependencies you need to install and also you gotta be more careful with how you cache the layers in order to get the optimal build time. So in my understanding, the best I can do is to tell you that you have these options and from here on, you can start exploring them on your own. |
Latest solution #384 (comment)
Edit 2017: check this solution #384 (comment)
Edit 2021: check this solution #384 (comment)
Edit 2021: check note on required dependencies: #384 (comment) and #384 (comment)
Edit 2022: the package
python
seems to have been deprecated/dropped a while ago in favor ofpython2
andpython3
, checkout this updated snippet #384 (comment)Recently I tried to dockerize a project which uses the
bcrypt
package. Everything works just fine on my mac but when I run it on Docker yarn throws a lot of errors related togyp
when trying to build the package during the installation process.Here's my Dockerfile:
Here's the output log:
I have opened this issue here because the error log mentioned that
python
wasn't found in the PATH so this is probably some error in node's 7.9-alpine Dockerfile, right?I also ran
sh
against the built image and checked by myself that there was nopython
orpython2
installed.Not sure if those information are enough, I'm kinda new to Docker and Node. Please let me know if I should provide more information.
The text was updated successfully, but these errors were encountered: