SensorsIot / IOTstack

Docker stack for getting started on IOT on the Raspberry PI
GNU General Public License v3.0
1.44k stars 304 forks source link

Open question for anyone who has a good understanding of npm #587

Open Paraphraser opened 2 years ago

Paraphraser commented 2 years ago

This is a request for guidance from anyone who has a good understanding of NPM (which isn't me).

I start at IOTstack/.templates/nodered/addons.yml. This is the directives file used as the basis for generating the Node-RED Dockerfile for IOTstack.

I want to focus on line 5 which is:

dockerFileInstallCommand: "RUN cd /usr/src/node-red && npm install --save "

If you run the menu, select Node-RED and accept the "default on" nodes, the Dockerfile you get is:

FROM nodered/node-red:latest-12
USER root
RUN apk update && apk add --no-cache eudev-dev
USER node-red

RUN cd /usr/src/node-red && npm install --save  node-red-configurable-ping
RUN cd /usr/src/node-red && npm install --save  node-red-contrib-boolean-logic
RUN cd /usr/src/node-red && npm install --save  node-red-contrib-influxdb
RUN cd /usr/src/node-red && npm install --save  node-red-dashboard
RUN cd /usr/src/node-red && npm install --save  node-red-node-pi-gpiod
RUN cd /usr/src/node-red && npm install --save  node-red-node-rbe

Question 1: why cd /usr/src/node-red?

The first command on each of the last 6 lines is cd /usr/src/node-red.

Our Dockerfile runs atop the Node-RED Dockerfile. Line 33 of the upstream Dockerfile sets the working directory:

WORKDIR /usr/src/node-red

and that is inherited when our IOTstack Dockerfile runs. It's easy enough to prove this by adding pwd && between the RUN and the cd:

 => [3/8] RUN pwd && cd /usr/src/node-red && npm install --save  node-red-configurable-ping
 => # /usr/src/node-red
 …

Question 2: why --save?

The Dockerfile generated by the menu uses the latest-12 tag but latest-14 and latest-16 tags are also available on DockerHub:

# build with latest-12
$ docker exec nodered bash -c 'node --version ; npm --version'
v12.22.8
6.14.15

# build with latest-14
$ docker exec nodered bash -c 'node --version ; npm --version'
v14.18.2
6.14.15

# build with latest-16
$ docker exec nodered bash -c 'node --version ; npm --version'
v16.13.1
8.1.2

I take that to mean that the earliest version of npm we need to consider is 6.14.15.

Practical experience

My own Dockerfile has looked like this for getting on for 2 years:

FROM nodered/node-red:latest-14
USER root
RUN apk update && apk add --no-cache eudev-dev mosquitto-clients bind-tools tcpdump tree
USER node-red
RUN npm install \ 
  node-red-node-pi-gpiod \
  node-red-dashboard \
  node-red-contrib-influxdb \
  node-red-contrib-boolean-logic \
  node-red-node-tail \
  node-red-configurable-ping \
  node-red-node-email

I have no change of directory ahead of the npm install and no --save option. Node-RED (the container, and all the add-ons, whether installed via Dockerfile or Manage Palette) have always just worked and have never given me a moment's trouble.

Is my conclusion correct that both the change of directory and the --save are redundant, or is there something about this that I'm missing?