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.