Why NPM is not available in Docker Container
Your fundamental problem is that you can only have exactly one CMD
in a Docker file. Each RUN
/COPY
command builds up a layer during docker build
, so you can have as many of those as you want. However, exactly one CMD
gets executed during a docker run
. Since you have three CMD
statements, only one of them actually gets executed (presumably, the last one).
(IMO, if the Dockerfile team would have chosen the word BUILD
instead of RUN
and RUN
instead of CMD
, so that docker build
does BUILD statements and docker run
does RUN statements, this might have been less confusing to new users. Oh, well.)
You either want to convert your first two CMD
s to RUN
s (if you expect them to happen during the docker build
and be baked into the image) or perhaps put all three CMD
s in a script that you run. Here's a few solutions:
(1) The simplest change is probably to use WORKDIR
instead of cd
and make your npm install
a RUN
command. If you want to be able to npm install
during building so that your server starts up quickly when you run, you'll want to do:
#Copy the sources to Container
COPY ./src /src
WORKDIR /src
RUN npm install
CMD nodejs server.js
(2) If you're doing active development, you may want to consider something like:
#Copy the sources to Container
WORKDIR /src
COPY ./src/package.json /src/package.json
RUN npm install
COPY /src /src
CMD nodejs server.js
So that you only have to do the npm install if your package.json changes. Otherwise, every time anything in your image changes, you rebuild everything.
(3) Another option that's useful if you're changing your package file often and don't want to be bothered with both building and running all the time is to keep your source outside of the image on a volume, so that you can run without rebuilding:
...
WORKDIR /src
VOLUME /src
CMD build_and_serve.sh
Where the contents of build_and_serve.sh
are:
#!/bin/bash
npm install && nodejs server.js
And you run it like:
docker run -v /path/to/your/src:/src -p 8080:8080 -d --name nodejs_expreriments nmrony/exp-nodejs
Of course, that last option doesn't give you a portable docker image that you can give someone with your server, since your code is outside the image, on a volume.
Lots of options!
For me this worked:
RUN apt-get update \
&& apt-get upgrade -y \
&& curl -sL https://deb.nodesource.com/setup_8.x | bash - \
&& apt-get install -y nodejs \
&& npm install -g react-tools
My debian image apt-get was getting a broken/old version of npm, so passing a download path fixed it.