Putting here both question and answer since I am not allowed to comment on post for I don't have 50 reputation.
Error
While building a docker image with node.js NPM tries to access registry "https://registry.npmjs.org/express" but it can't find the root certificate that a "network proxy " demands for verification before forwarding the request to internet.
> [6/7] RUN npm install:
0.870 npm ERR! code UNABLE_TO_GET_ISSUER_CERT_LOCALLY
0.870 npm ERR! errno UNABLE_TO_GET_ISSUER_CERT_LOCALLY
0.873 npm ERR! request to https://registry.npmjs.org/express failed, reason: unable to get local issuer certificate
0.881
0.881 npm ERR! A complete log of this run can be found in:
0.881 npm ERR! /root/.npm/_logs/2024-11-24T00_00_24_287Z-debug.log
------
Dockerfile:17
--------------------
15 |
16 | >>> RUN npm install
17 |
18 | COPY . .
--------------------
ERROR: failed to solve: process "/bin/sh -c npm install" did not complete successfully: exit code: 1
Answer
The NPM needs a root certificate for verification by proxy before proxy allows it to go out to internet.
More secure way then turning off ssl check!
npm config set strict-ssl
or Running this which did not work.
npm config set registry http://registry.npmjs.org/
is as mentioned in the code, copy this to your VScode.
FROM node:14
# Here you are setting the environment variable for your docker environment, no certificate existing yet!
ARG NODE_EXTRA_CA_CERTS=/etc/ssl/certs/ca-certificates.crt
# Export and save your Org/company's root certificate (./certificates.crt) to your DOCKERFILE location (In WSL copy to \\wsl$\home\wsl\location)
COPY ./Certificate.crt ./usr/local/share/ca-certificates/
# Now the contents are being copied to default location of certificates where npm will look, its same location as set in env variable of docker.
RUN cat /usr/local/share/ca-certificates/Certificate.crt >>/etc/ssl/certs/ca-certificates.crt
WORKDIR /app
COPY package.json .
RUN npm install
COPY . .
EXPOSE 3000
CMD [ "node", "app.mjs" ]
[Note: Do check https://www.docker.com/blog/docker-best-practices-using-arg-and-env-in-your-dockerfiles/]