Npm install in Docker tutorial is taking FOREVER

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

# 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


# 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:
 => => # 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:

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