Npm install in Docker tutorial is taking FOREVER

Hi,
I’m following the basic Docker tutorials and now executing the following to create and image:

# Start your image with a node base image
FROM node:18-alpine

# The /app directory should act as the main application directory
WORKDIR /app

# Copy the app package and package-lock.json file
COPY package*.json ./

# Copy local directories to the current local directory of our docker image (/app)
COPY ./src ./src
COPY ./public ./public

# Install node packages, install serve, build the app, and remove dependencies at the end
RUN npm install \
    && npm install -g serve \
    && npm run build \
    && rm -fr node_modules



EXPOSE 3000

# Start the app using serve command
CMD [ "serve", "-s", "build" ]

The result is that it’s stuck at “npm install” and not seeming to progress (unless this is supposed to take a couple hours which I’ve not tried yet).

~/welcome-to-docker$ docker build -t welcome-to-docker-test .
[+] Building 1371.8s (9/10)                                                                                               docker:desktop-linux
 => [internal] load build context                                                                                                         0.0s
 => => transferring context: 393B                                                                                                         0.0s
 => CACHED [2/6] WORKDIR /app                                                                                                             0.0s
 => CACHED [3/6] COPY package*.json ./                                                                                                    0.0s
 => CACHED [4/6] COPY ./src ./src                                                                                                         0.0s
 => CACHED [5/6] COPY ./public ./public                                                                                                   0.0s
 => [6/6] RUN npm install     && npm install -g serve     && npm run build     && rm -fr node_modules                                  3370.8s
 => => # npm WARN deprecated stable@0.1.8: Modern JS already guarantees Array#sort() is a stable sort, so this library is deprecated. See the 
 => => # compatibility table on MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#browser_compa
 => => # tibility                                                                                                                             
 => => # npm WARN deprecated rollup-plugin-terser@7.0.2: This package has been deprecated and is no longer maintained. Please use @rollup/plug
 => => # in-terser                                                                                                                            
 => => # npm WARN deprecated svgo@1.3.2: This SVGO version is no longer supported. Upgrade to v2.x.x.                                         

Neither CPU, Memory or network seem to be the bottleneck:

1 Like

Try to split this

into multiple lines

RUN npm install
RUN npm install -g serve
RUN npm run build
RUN rm -fr node_modules

to debug further.

I find the deletion of your node_imodules folder a bit strange. Your app needs no dependencies besides global serve?

I don’t even know why I’m doing these things. I’m just following what the tutorial says.
The code is stuck at RUN npm install.

Anyway,I figured maybe this is not docker related but a problem with npm ?
The other parts of the tutorials work fine.

It worked for me after running this:

Clear all builder instances

  • docker builder prune

Restart docker

  • sudo systemctl restart docker

Hey there,
two month later and the issue is still there, or did anyone found a solution for this?

The previous mentioned thing did not work for me. npm install inside Dockerfile is insanelyy slow.

Did you try changing the node version?
I was facing the same issue with node:20.12.2-alpine, but it was fixed when I used FROM node:18.20.2-alpine

Hello jhaanKit Ankit ,
What docker version were you using? facing the same error

I have the same issue with Docker version 24.0.2 (after the upgrade, but I also tried with an older version, I don’t remember which) and the package node:18. I also tried with node:16.20 and node:17.8.

I faced a similar problem, on my virtual machine everything worked Ubuntu, but on Windows
there was a problem

  1. I added to RUN npm install --force --loglevel verbose, logging level and saw that I had a problem with the certificate
  2. => => # npm http fetch GET https://registry.npmjs.org/npm attempt 1 failed with SELF_SIGNED_CERT_IN_CHAIN
  3. Before NPM install I added RUN npm config set strict-ssl false
    And it worked
4 Likes

Had the same issue the problem was the WORKDIR that wasn’t correct

Thanks , Facing this issue for hours and your solution worked For me :heart:

1 Like

I didn’t have this issue until recently. I think it was possibly introduced in a recent Docker update? Same version of node, same package.json, same base image, but now ‘npm install’ is taking ridiculously long to install dependencies.
However, fortunately:
RUN npm config set strict-ssl false
Also fixed it for me.

2 Likes

I face the same issue and it’s like Tom said: It worked previously and nothing changed. I just noticed because I wanted to build an image with the no-cache flag and it took forever: npm install ~ 30 minutes, the following npm install @angular/cli over an hour! After that, using the cache again it was all good.

I retried this with the suggested fix. This brought npm install down to a minute, but the angular cli was still endless. I killed the process after two hours.

I really hope somebody can figure out, what this issue is. My knowledge is enough to use things, but not to troubleshoot it and this is really slowing things down quite literally.

Unfortunately setting npm strict-ssl config to false doesn’t speed things up for me.

The interesting thing is that my image builds perfectly fine on my mac and linux machines, just not windows.

image

Doesn’t seem to be a general problem.

Since no one shared a link to the used tutorial or an example project that illustrates the problem, I used https://docs.docker.com/get-started/workshop/02_our_app/#get-the-app as source.

S D:\docker-workshop\getting-started-app> docker build -t getting-started .
[+] Building 27.3s (11/11) FINISHED                                                                                          docker:desktop-linux

Please share how to reproduce your issues.

I have the same issue. Started to observe this issue when switched to the new Internet provider. If I switch to my mobile internet “npm install” works. So it looks like some network firewall or something blocks the request.
“strict-ssl false” also didn’t help

Stumbled across this thread while searching for the same issue, so it might be connected. I have currently the same issue in building Migrate code to Typescript and eslint flat config by jopicornell · Pull Request #91 · janjaali/sendGrid-mock · GitHub with latest Docker version 27.3.1, build ce12230 on Windows, while the same build is running green on GitHub Actions using Ubuntu. The mentioned RUN npm config set strict-ssl false although helped already in resolving the issue in short run for me.

Total build time for the image is 44.9s. Pulling packages was 23.3s.

Tested on Win11 Enterprise, Intel Core Ultra 7 155H CPU, Wi-Fi 6E WLAN connection to a router with 1Gbps down stream fiber connection.

PS D:\dev\test> git clone https://github.com/janjaali/sendGrid-mock.git
Cloning into 'sendGrid-mock'...
remote: Enumerating objects: 1158, done.
remote: Counting objects: 100% (321/321), done.
remote: Compressing objects: 100% (120/120), done.
remote: Total 1158 (delta 202), reused 282 (delta 180), pack-reused 837 (from 1)
Receiving objects: 100% (1158/1158), 985.03 KiB | 5.86 MiB/s, done.
Resolving deltas: 100% (548/548), done.
PS D:\dev\test> cd .\sendGrid-mock\
PS D:\dev\test\sendGrid-mock> docker build -t ghashange/sendgrid-mock:1.12.0 .
[+] Building 44.9s (21/21) FINISHED                                                                                            docker:desktop-linux
 => [internal] load build definition from Dockerfile                                                                                           0.1s
 => => transferring dockerfile: 1.66kB                                                                                                         0.0s
 => WARN: FromAsCasing: 'as' and 'FROM' keywords' casing do not match (line 1)                                                                 0.1s
 => [internal] load metadata for docker.io/library/node:16-alpine                                                                              5.3s
 => [auth] library/node:pull token for registry-1.docker.io                                                                                    0.0s
 => [internal] load .dockerignore                                                                                                              0.1s
 => => transferring context: 81B                                                                                                               0.0s
 => [node 1/1] FROM docker.io/library/node:16-alpine@sha256:a1f9d027912b58a7c75be7716c97cfbc6d3099f3a97ed84aa490be9dee20e787                   8.8s
 => => resolve docker.io/library/node:16-alpine@sha256:a1f9d027912b58a7c75be7716c97cfbc6d3099f3a97ed84aa490be9dee20e787                        0.0s
 => => sha256:a1f9d027912b58a7c75be7716c97cfbc6d3099f3a97ed84aa490be9dee20e787 1.43kB / 1.43kB                                                 0.0s
 => => sha256:72e89a86be58c922ed7b1475e5e6f151537676470695dd106521738b060e139d 1.16kB / 1.16kB                                                 0.0s
 => => sha256:2573171e0124bb95d14d128728a52a97bb917ef45d7c4fa8cfe76bc44aa78b73 6.73kB / 6.73kB                                                 0.0s
 => => sha256:7264a8db6415046d36d16ba98b79778e18accee6ffa71850405994cffa9be7de 3.40MB / 3.40MB                                                 0.4s
 => => sha256:eee371b9ce3ffdbb8aa703b9a14d318801ddc3468f096bb6cfeabbeb715147f9 36.63MB / 36.63MB                                               6.9s
 => => sha256:93b3025fe10392717d06ec0d012a9ffa2039d766a322aac899c6831dd93382c2 2.34MB / 2.34MB                                                 0.5s
 => => extracting sha256:7264a8db6415046d36d16ba98b79778e18accee6ffa71850405994cffa9be7de                                                      0.1s
 => => sha256:d9059661ce70092af66d2773666584fc8addcb78a2be63f720022f4875577ea9 452B / 452B                                                     0.6s
 => => extracting sha256:eee371b9ce3ffdbb8aa703b9a14d318801ddc3468f096bb6cfeabbeb715147f9                                                      1.6s
 => => extracting sha256:93b3025fe10392717d06ec0d012a9ffa2039d766a322aac899c6831dd93382c2                                                      0.1s
 => => extracting sha256:d9059661ce70092af66d2773666584fc8addcb78a2be63f720022f4875577ea9                                                      0.0s
 => [internal] load build context                                                                                                              0.5s
 => => transferring context: 2.01MB                                                                                                            0.4s
 => [stage-2 1/9] RUN apk --no-cache -U upgrade                                                                                                1.9s
 => [builder 1/5] WORKDIR /app                                                                                                                 0.1s
 => [builder 2/5] COPY package*.json ./                                                                                                        0.0s
 => [builder 3/5] RUN npm ci                                                                                                                  23.3s
 => [stage-2 2/9] RUN apk add --no-cache certbot tini                                                                                          3.8s
 => [stage-2 3/9] COPY letsencrypt_webroot.sh /                                                                                                0.0s
 => [stage-2 4/9] RUN chmod +x /letsencrypt_webroot.sh                                                                                         0.5s
 => [stage-2 5/9] WORKDIR /usr/src/server                                                                                                      0.0s
 => [stage-2 6/9] COPY package*.json ./                                                                                                        0.0s
 => [stage-2 7/9] RUN npm i --only=production                                                                                                  5.1s
 => [builder 4/5] COPY . .                                                                                                                     0.0s
 => [builder 5/5] RUN npm run build                                                                                                            6.1s
 => [stage-2 8/9] COPY --from=builder /app/dist ./dist                                                                                         0.0s
 => [stage-2 9/9] COPY src/server ./src/server                                                                                                 0.0s
 => exporting to image                                                                                                                         0.6s
 => => exporting layers                                                                                                                        0.5s
 => => writing image sha256:f9dddd5a03884c7429db5341813179ab862da2a342348b708ac98accc4eed2d6                                                   0.0s
 => => naming to docker.io/ghashange/sendgrid-mock:1.12.0                                                                                      0.0s

View build details: docker-desktop://dashboard/build/desktop-linux/desktop-linux/zmq3akxvit3703hn51focij4m

 2 warnings found (use docker --debug to expand):
 - FromAsCasing: 'as' and 'FROM' keywords' casing do not match (line 1)
 - SecretsUsedInArgOrEnv: Do not use ARG or ENV instructions for sensitive data (ENV "API_KEY") (line 53)

What's next:
    View a summary of image vulnerabilities and recommendations → docker scout quickview

If I re-run the build with --no-cache it is even faster, because the base image doesn’t need to be pulled again:

PS D:\dev\test\sendGrid-mock> docker build -t ghashange/sendgrid-mock:1.12.0 --no-cache .
[+] Building 31.5s (21/21) FINISHED

Same for me for that version, but maybe you want to try the Dockerfile version I have linked in initial message. The one from the PR :wink:

I was having this issue. I tested running npm install inside the project and had the same issue. Therefore, the problem was not with Docker.

I use a vpn from a major provider. Disconnected the VPN and ran npm install again and this was solved for me.