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 · 14 comments
Labels

Comments

@LaurentGoderre
Copy link
Contributor

@LaurentGoderre LaurentGoderre commented Dec 6, 2016

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

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link
Member

@SimenB SimenB commented Sep 22, 2017

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

@LaurentGoderre

This comment has been minimized.

Copy link
Contributor Author

@LaurentGoderre LaurentGoderre commented Sep 22, 2017

@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

This comment has been minimized.

Copy link

@dan-turner dan-turner commented Sep 22, 2017

@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

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link
Contributor Author

@LaurentGoderre LaurentGoderre commented Jan 19, 2018

@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

This comment has been minimized.

Copy link

@bishoymelek bishoymelek commented Nov 7, 2018

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

@m1kola

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link
Member

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

This comment has been minimized.

Copy link
Member

@SimenB SimenB commented Nov 7, 2018

@bnb yeah!

@dm17

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link
Contributor Author

@LaurentGoderre LaurentGoderre commented Jul 22, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
9 participants
You can’t perform that action at this time.