When using alpine, you need to install build dependencies for some node module to be able to be built natively. It should probably be documented
FROM node:alpine
RUN apk add --no-cache --virtual .gyp \
python \
make \
g++ \
&& npm install \
[ your npm dependencies here ] \
&& apk del .gyp
How could one do this with dockers multi-stage builds instead?
The above approach requires everything to be done in one run command after the app has been copied in in an earlier layer. This means the apk add step can't be cached.
We should definitely have examples with multi-stage builds as well
@dan-turner this issues pre-dates the multi stages build. We should have examples of multi stage build including how to use node-gyp.
@simenb @laurentgoderre no problem! Sorry I didn鈥檛 mean to sound ungrateful, I ended using the above example today because it worked perfectly :)
I had a crack at trying to work out how to do it with multi-stage builds myself but it鈥檚 beyond my expertise I鈥檓 afraid.
Had the same issue today. Here is simplified multi-stage build example:
FROM node:8-alpine AS assets
WORKDIR /app
# Install yarn and other dependencies via apk
RUN apk update && apk add yarn python g++ make && rm -rf /var/cache/apk/*
# Install node dependencies - done in a separate step so Docker can cache it.
COPY package.json yarn.lock /app/
RUN yarn install --frozen-lockfile
# Copy project files into the docker image
COPY . /app/
# Build something. Let's assume that it outputs into /app/dist/
RUN yarn build:prod
# Build an python app (or something else) and copy from the previous stage
FROM python:3.6.3
# ...
COPY --from=assets /app/dist/ /app/dist/
# ...
I found this very helpful: https://github.com/gliderlabs/docker-alpine/blob/master/docs/usage.md
My Dockerfile looks like this:
FROM node:9-alpine
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY . /usr/src/app
# --no-cache: download package index on-the-fly, no need to cleanup afterwards
# --virtual: bundle packages, remove whole bundle at once, when done
RUN apk --no-cache --virtual build-dependencies add \
python \
make \
g++ \
&& npm install \
&& apk del build-dependencies
EXPOSE 3000
CMD [ "npm", "start" ]
Using --no-cache avoids having to use rm -rf /var/cache/apk/*; using --virtual allows you to remove multiple packages required during build only at once.
@sepastian the multi-stage build allows for even smaller image by running a different image for building and for running so you don't inherit all the build dependencies in your runtime image.
we are almost in 2019 and it's still not documented :-/
@bishoymelek it's an open source project and you can help improve it. Don't hesitate to take the lead and update the documentation 馃槈
Hey y'all! Would this be a good candidate for the label "good first issue"? Seems like the majority of the code content needed is in the OP.
@bnb yeah!
Is this still the correct work around for node on alpine 12.6? Am I correct that the following block replaces "RUN npm install"?
# --no-cache: download package index on-the-fly, no need to cleanup afterwards
# --virtual: bundle packages, remove whole bundle at once, when done
RUN apk --no-cache --virtual build-dependencies add \
python \
make \
g++ \
&& npm install \
&& apk del build-dependencies
@sepastian works for me, but aren't you getting these warnings:
https://github.com/gliderlabs/docker-alpine/issues/207
I'm going to have to mix your suggestion and the ones in that thread...
Running apk --no-cache --update --virtual build-dependencies add ... should remove the warning.
Most helpful comment
I found this very helpful: https://github.com/gliderlabs/docker-alpine/blob/master/docs/usage.md
My
Dockerfilelooks like this:Using
--no-cacheavoids having to userm -rf /var/cache/apk/*; using--virtualallows you to remove multiple packages required during build only at once.