How would I update node.js inside of Docker container on Windows - node.js

I am running into a problem finding good documentation on how to update and check the version of Node that runs on my Docker container. This is just a regular container for .net 2 application. Thank you in advance.

To answer your questions directly:
If you don't have the Image's Dockerfile:
You can probably (!) determine the version of NodeJS in your container by getting a shell into the container and then running node --version or perhaps npm --version. You could try docker run .... --entrypoint=bash your-container node --version
You can change a running Container and then use docker commit to create a new Container Image from the result.
If you do have the Image's Dockerfile:
You should be able to review it to determine which version of NodeJS it installs.
You can update the NodeJS version in the Dockerfile and rebuild the Image.
It's considered bad practice to update images and containers directly (i.e. not updating the Dockerfile).
Dockerfiles are used to create Images and Images are used to create containers. The preferred approach is to update the Dockerfile to update the Image and to use the updated Image to create update Containers.
The main reason is to ensure reproducibility and to provide some form of audit in being able to infer the change from the changed Dockerfile.

In your Dockerfile
FROM node:14-alpine as development

Related

Best way to build a Docker image

I have just started to learn Docker. I have a nodeJs web application and built 2 images of it using 2 different Dockerfile. The 2 images are really different in size. Can someone tell me why they are really different in size even though they use the same alpine, node, and npm version, and which one is the recommended way to build a nodeJS application image?
First Dockerfile which created a 91.92MB image:
FROM alpine
RUN apk add --update nodejs npm curl
COPY . /src
WORKDIR /src
RUN npm install
EXPOSE 8080
ENTRYPOINT ["node", "./app.js"]
Second Dockerfile which created a 259.57MB image:
FROM node:19-alpine3.16
RUN apk add --update npm curl
COPY . /src
WORKDIR /src
RUN npm install
CMD [ "node", "./app.js" ]
If it meets your technical and non-technical needs, using the prebuilt node image is probably simpler and easier to update, and I'd generally recomment using it.
The node image already includes npm; you do not have to apk add it. Moreover, the Dockerfile doesn't use apk to install Node, but instead installs it either from a tar file or from source. So the apk add npm call winds up installing an entire second copy of Node that you don't need; that's probably the largest difference in the two image sizes.
The base image you start from hugely affects your final image size - assuming you are adding the same stuff to each base image. In your case, you are comparing a base alpine image (5.5mb) to a base node image (174mb). The base alpine image will have very little stuff in it while the node image - while based on alpine - has at least got node in it but presumably lots of extra stuff too. Whether you do or do not need that extra stuff is not for me to say.
You can examine the Dockerfile used to build any of these public images if you wish to see exactly what was added. You can also use tools like dive to examine a local image layers.

How to add node.js to this docker-compose file that spins up SonarQube?

I've installed a local SonarQube server in Docker on my machine, using this docker-compose.yml based on this recipe. It spins up a Postgres database backend as well as SonarQube itself.
When I run analysis of a Java project through Maven, it analyzes everything except my project's JS and CSS. I get these warnings:
CSS files were not analyzed. Error when running: 'node -v'. Is Node.js available during analysis?
Some JavaScript rules were not executed. Error when running: 'node -v'. Is Node.js available during analysis?
SonarQube's documentation explains: "In order to analyze CSS code, you need to have Node.js >= 8 installed on the machine running the scan. Set property sonar.nodejs.executable to an absolute path to Node.js executable, if standard node is not available."
My question for you Docker-Compose experts is: How can I incorporate Node.js into the docker-compose configuration? (So I can get the benefits of these analyses without having to install and configure Node.js on the host machine outside of Docker...)
At the same level as your compose file, create a Dockerfile
Note1: For example simplicity, I'm building against the latest tag by default which is a bad practice for production. You should pick the version that suits your needs (e.g. lts, 9.2.4-developer....)
Note2: For sonarqube versions prior to 8, the official image was based on an ubuntu image. Since version 8, the base image has been switch to alpine. I kept the previous example Dockerfile below
Dockerfile for sonar >= 8
FROM sonarqube
USER root
RUN apk --no-cache add nodejs
USER sonarqube
Dockerfile for sonar < 8 (for memory)
FROM sonarqube
USER root
RUN apt-get update \
&& apt-get install -y --no-install-recommends nodejs \
&& apt-get clean
USER sonarqube
Modify your compose file to build and use this new image (note that the image name must change).
sonarqube:
image: my_local_sonarqube
build: .
ports:
- "9000:9000"
networks:
- sonarnet
environment:
- sonar.jdbc.url=jdbc:postgresql://db:5432/sonar
volumes:
- sonarqube_conf:/opt/sonarqube/conf
- sonarqube_data:/opt/sonarqube/data
- sonarqube_extensions:/opt/sonarqube/extensions
Launch your app. The image will be built automatically since it does not exists. If you need to rebuild the image later (e.g. after a change to the Dockerfile), you need to either do it manually with docker-compose build or to use the --build option to docker-compose up.

How to to use Docker for ElectronJS app built in Windows?

I am working on a desktop application build using ElectronJS framework on Windows.
May be I haven't understood Docker properly, but how do I use docker for this app?
My end goal is to not let people install node, npm and electron packages on their local system. They can use the docker image to develop this application.
Update
I figured out how to package my project in docker image. Now I am struggling to run the app through the Docker Container.
How to run a GUI(Electron Application) application using docker container?
Thanks.
In your case you need custom docker images that will have node, npm and ElectronJS in it.
I found one such image on dockerhub, you can use it or build your own custom image by checking its dockerfile.

What is docker node:9-stretch?

I'm looking at a Dockefile we have in one of our projects that essentially builds our UI.
I see it's using FROM node:9-stretch which ships with npm 5.6.0. I want to use npm ci which requires 5.7.0 so i need to update my dockerfile node base image.
I do not see a node:9-stretch in docker hub https://hub.docker.com/_/node/
Where is this pulling node:9-stretch from and what is a -stretch version of a base image?
The node 9 build was dropped after this commit https://github.com/nodejs/docker-node/commit/b22fb6c84e3cac83309b083c973649b2bf6b092d. You can find Dockerfile in diff.
The node:9-stretch image you can pull build before the commit, and persisted in docker hub. The 9-stretch tag exists in Tags page, as for now https://hub.docker.com/_/node?tab=tags&page=18.

How to restart Node on a docker container without restarting the whole container?

I have a container ==> FROM node:5
Node should restart after each change in the code.
But there is no way for me restart the Node server without restarting the whole docker container.
I have many npm install on dockerfile that runs each time I restart the container, and it's annoying to wait for all of them after each change in the code.
I'm already using shared folder to have the latest code in my container.
If you just need the Node.js server to restart after changes, instead of starting the server with the node command you can use a tool like node-dev or nodemon. To leverage these, you'd need to update the command in your Dockerfile, e.g. CMD [ "node-dev", "server.js" ].
Since you already have your local files as a volume in the Docker container, any change to those files will restart the server.
Here's how I did it. In your docker-compose.yml, in the entry for the appropriate service, add an entrypoint to run npx nodemon /path/to/your/app
This will work even if you don't have nodemon installed in the image.
e.g.
services:
web:
image: foo
entrypoint: ["npx", "nodemon", "/usr/src/app/app.js"]
I think that's not the optimal way to Docker. You should try to build your own Docker image which includes your code changes. In your own Docker image you could make the npm install step part of the container build so you do not need to run this command on container startup.

Resources