2

I have dockerfile which has base image go and I install npm/node:

FROM golang:1.7
RUN apt-get update && apt-get install -y wget

###node
ENV NVM_DIR /usr/local/nvm
ENV NODE_VERSION 6.10.1

RUN wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.31.3/install.sh | bash \
    && . $NVM_DIR/nvm.sh \
    && nvm install $NODE_VERSION \
    && nvm alias default $NODE_VERSION \
    && nvm use default

ENV NODE_PATH $NVM_DIR/v$NODE_VERSION/lib/node_modules
ENV PATH      $NVM_DIR/v$NODE_VERSION/bin:$PATH

When I start this container I can perform node or npm commands inside the container:

docker exec -it 763993cc1f7a bash
root@763993cc1f7a:/go# npm -v
3.10.10

But when I add a node or npm command to the dockerfile:

RUN npm ...

I get: /bin/sh: 1: npm: not found How is this possible?

8
  • 2
    This might be your problem stackoverflow.com/questions/25899912/install-nvm-in-docker Commented Aug 28, 2017 at 9:02
  • @yamenk yep it is! Commented Aug 28, 2017 at 9:19
  • 2
    Why are you building a Node image from Go base? Commented Aug 28, 2017 at 9:20
  • @TarunLalwani Because I need both in one image. I didn't show the go part of my Dockerfile. For local development we use a go containd and a node container but his works only locally for us. In further stages we have to keep the javascript inside the go Commented Aug 28, 2017 at 10:46
  • Having you looked at the multi stage build? docs.docker.com/engine/userguide/eng-image/multistage-build/… Commented Aug 28, 2017 at 10:48

1 Answer 1

3

You need to avoid using NVM. You can do this using Multi stage dockerfile in your code. Assuming Go is the main app and npm is needed for webpack or other build activity

So you final docker file should be something like below

ARG NODE_VERSION
FROM node:${NODE_VERSION} as static
...
RUN webpack build


FROM go:1.7
COPY --from=static /app/static /app/static
....
CMD ["./goapp"]

This feature was introduce in Docker 17.05 ce, so you will need the latest version.

Sign up to request clarification or add additional context in comments.

1 Comment

Very easy to use, reduced my image size by 150MB

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.