Skip to content
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

Document how to use alpine with dependencies that rely on node-gyp #282

Closed
LaurentGoderre opened this issue Dec 6, 2016 · 20 comments
Closed

Comments

@LaurentGoderre
Copy link
Member

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
@dan-turner
Copy link

dan-turner commented Sep 22, 2017

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.

@SimenB
Copy link
Member

SimenB commented Sep 22, 2017

We should definitely have examples with multi-stage builds as well

@LaurentGoderre
Copy link
Member Author

@dan-turner this issues pre-dates the multi stages build. We should have examples of multi stage build including how to use node-gyp.

@dan-turner
Copy link

@SimenB @LaurentGoderre no problem! Sorry I didn’t 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’s beyond my expertise I’m afraid.

@m1kola
Copy link

m1kola commented Jan 8, 2018

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/

# ...

@sepastian
Copy link

sepastian commented Jan 19, 2018

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.

@LaurentGoderre
Copy link
Member Author

@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.

@bishoymelek-zz
Copy link

we are almost in 2019 and it's still not documented :-/

@m1kola
Copy link

m1kola commented Nov 7, 2018

@bishoymelek it's an open source project and you can help improve it. Don't hesitate to take the lead and update the documentation 😉

@bnb
Copy link

bnb commented Nov 7, 2018

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.

@SimenB
Copy link
Member

SimenB commented Nov 7, 2018

@bnb yeah!

@dm17
Copy link

dm17 commented Jul 10, 2019

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

@dm17
Copy link

dm17 commented Jul 21, 2019

@sepastian works for me, but aren't you getting these warnings:
gliderlabs/docker-alpine#207

I'm going to have to mix your suggestion and the ones in that thread...

@LaurentGoderre
Copy link
Member Author

Running apk --no-cache --update --virtual build-dependencies add ... should remove the warning.

@kdalkafoukis
Copy link

I managed to fix the error with
RUN apk add --no-cache --virtual .gyp python make g++ pkgconfig pixman-dev cairo-dev pango-dev
for me to work in
docker node alpine

I had to follow the errors and add the packages pkgconfig pixman-dev cairo-dev pango-dev to the already existing solutions like #282 (comment)

xmflsct added a commit to tooot-app/services that referenced this issue Jun 21, 2021
@alexhammerschmied
Copy link

I just tried to use the command:

apk --no-cache --virtual build-dependencies add \ python \ make \ g++ \ && npm i n8n-nodes-dnc-suitecrm --unsafe-perm \ && apk del build-dependencies

and got this:

ERROR: ' python' is not a valid child dependency, format is name([<>~=]version)

maybe the pr from @xmflsct is the thing to go with?

@LaurentGoderre
Copy link
Member Author

I believe these instructions worked for the original version of Alpine we supported but newer versions might need a different set of dependencies.

@KiSchulte
Copy link

@alexhammerschmied Have you found a fix for this issue? I've the same problem on one build right now.

@InclinedScorpio
Copy link

Anyone can share a working solution for this, please? I am using node14 and keep getting python error.

@nschonni
Copy link
Member

nschonni commented Dec 8, 2021

@nodejs nodejs locked as resolved and limited conversation to collaborators Dec 8, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests