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?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
DenCowboy
  • 13,884
  • 38
  • 114
  • 210
  • 2
    This might be your problem https://stackoverflow.com/questions/25899912/install-nvm-in-docker – yamenk Aug 28 '17 at 09:02
  • @yamenk yep it is! – DenCowboy Aug 28 '17 at 09:19
  • 2
    Why are you building a Node image from Go base? – Tarun Lalwani Aug 28 '17 at 09: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 – DenCowboy Aug 28 '17 at 10:46
  • Having you looked at the multi stage build? https://docs.docker.com/engine/userguide/eng-image/multistage-build/#name-your-build-stages – Tarun Lalwani Aug 28 '17 at 10:48
  • @TarunLalwani omg, that's the solution!? I can just build my go app and build the node part and copy the necessary files from node to go in 1 dockerfile? – DenCowboy Aug 28 '17 at 10:49
  • Yes, looking at your Dockerfile, I thought you don't know about this latest feature – Tarun Lalwani Aug 28 '17 at 10:50
  • @TarunLalwani very cool. So my 'builder' will be node? and after that I describe my Go setup and I copy the necessary files? – DenCowboy Aug 28 '17 at 10:50

1 Answers1

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.

Tarun Lalwani
  • 142,312
  • 9
  • 204
  • 265