'update npm in docker

I have nodejs running in docker. Every time I do npm run dev I see npm notice that I should update npm version, but when I try to do it, it is not updated anyway. How to make it updated?

root@Server:# docker-compose run --rm "nodejs" npm run dev
Creating webserver_nodejs_run ... done

> dev
> npm run development

> development
> mix

● Mix █████████████████████████ emitting (98%)
 after emit


● Mix █████████████████████████ done (99%) plugins
 WebpackBar:done


✔ Mix
  Compiled successfully in 28.36s

   Laravel Mix v6.0.34

✔ Compiled Successfully in 27297ms
┌───────────────────────────────────────────────────────────────────┬──────────┐
│                                                              File │ Size     │
├───────────────────────────────────────────────────────────────────┼──────────┤
│                                                        /js/app.js │ 1.13 MiB │
│                                                       css/app.css │ 8.16 MiB │
│       …/lightgallery/dist/lg.svg?adb7aeef16adb8599e2d261b9a3a9f1e │ 9.64 KiB │
│       …/lightgallery/dist/lg.ttf?a53dcc5ea47fe7d1d9fc881ff489f409 │ 4.33 KiB │
│       …lightgallery/dist/lg.woff?0add59587d07387b771f49d3f91a92af │ 4.4 KiB  │
│       …tgallery/dist/loading.gif?fcba57cdb89652f9bb54271cc5a9cc0e │ 4.08 KiB │
│       …llery/dist/video-play.png?539c47b61bf24fb7ea0ca1953952a0e4 │ 4.42 KiB │
│       …llery/dist/vimeo-play.png?6b92efbd0d64acd8b405e4fc4342305d │ 5.3 KiB  │
│       …ery/dist/youtube-play.png?50359884d4f563065161d9c7ea0ad585 │ 5.06 KiB │
└───────────────────────────────────────────────────────────────────┴──────────┘

● Mix █████████████████████████ done (99%)
 plugins

WARNING in DefinePlugin
Conflicting values for 'process.env.NODE_ENV'

2 WARNINGS in child compilations (Use 'stats.children: true' resp. '--stats-children' for more details)
webpack compiled with 3 warnings
npm notice
npm notice New minor version of npm available! 8.5.5 -> 8.10.0
npm notice Changelog: https://github.com/npm/cli/releases/tag/v8.10.0
npm notice Run npm install -g [email protected] to update!
npm notice
root@Server:# docker-compose run --rm "nodejs" npm install -g [email protected]
Creating webserver_nodejs_run ... done

removed 6 packages, changed 74 packages, and audited 202 packages in 5s

11 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities
npm notice
npm notice New minor version of npm available! 8.5.5 -> 8.10.0
npm notice Changelog: https://github.com/npm/cli/releases/tag/v8.10.0
npm notice Run npm install -g [email protected] to update!
npm notice

In node Dockerfile I have this. I don't use latest tag, because after update to latest npm run dev compiled with errors. So I returned to node v16.

FROM node:lts

 RUN deluser --remove-home node \
  && groupadd --gid 1023 http \
  && useradd --uid 1023 --gid http --shell /bin/bash --create-home http \
  && mkdir -p /home/http/app \
  && chown -R http:http /root \
  && chown -R http:http /usr/local \
  && chown -R http:http /home

then docker-compose

  nodejs:
    container_name: nodejs
    build:
      context: ./nodejs
    user: "http"
    working_dir: /home/http/app
    environment:
      - NODE_ENV=dev
    volumes:
      - ${PROJECTS}/myapp.lan:/home/http/app
    expose:
      - "8081"
    command: "npm start"


Solution 1:[1]

You add the npm install command to your Dockerfile like this

FROM node:lts

 RUN deluser --remove-home node \
  && groupadd --gid 1023 http \
  && useradd --uid 1023 --gid http --shell /bin/bash --create-home http \
  && mkdir -p /home/http/app \
  && chown -R http:http /root \
  && chown -R http:http /usr/local \
  && chown -R http:http /home \
  && npm install -g [email protected]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1