Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker build takes long time for nodejs application

Tags:

node.js

docker

I am experiencing long build times for nodejs applications when building image with docker build command.

There is one big hang that takes couple of minutes

08:03:15  Step 1/11 : FROM node:14.1.0-alpine AS build
08:03:15   ---> 0854fcfc1637
08:03:15  Step 2/11 : COPY server/package*.json /nodejs/server/
08:03:15   ---> Using cache
08:03:15   ---> 4996283ff991
08:03:15  Step 3/11 : WORKDIR /nodejs/server
08:03:15   ---> Using cache
08:03:15   ---> 93e5b63fa81d
08:03:15  Step 4/11 : RUN npm ci
08:03:15   ---> Using cache
08:03:15   ---> 2c825e02ea01
08:03:15  Step 5/11 : COPY server ./
08:03:15   ---> Using cache
08:03:15   ---> 69c024cde79f
08:03:15  Step 6/11 : WORKDIR /nodejs
08:03:15   ---> Using cache
08:03:15   ---> 49d7f8bd9514
08:03:15  Step 7/11 : COPY package*.json ./
08:03:16   ---> e82bee625c3e
08:03:16  Step 8/11 : RUN npm ci
08:03:16   ---> Running in ecfd57702906
...
08:03:49  added 1483 packages in 26.419s
08:09:40  Removing intermediate container ecfd57702906
...
08:09:40   ---> 7c6b67d85b0b
08:09:40  Step 9/11 : COPY *.json ./
08:09:43   ---> 0165efd1c97d
08:09:43  Step 10/11 : COPY src ./src/
08:09:51   ---> 42e54cee6b91
08:09:51  Step 11/11 : RUN npm run build:prod
08:09:51   ---> Running in af6f9b013d27

This does not happen when building Java images.

My Dockerfile

FROM node:14.1.0-alpine AS build
COPY server/package*.json /nodejs/server/
WORKDIR /nodejs/server
RUN npm ci
COPY server ./

WORKDIR /nodejs
COPY package*.json ./
RUN npm ci
COPY *.json ./
COPY src ./src/
RUN npm run build:prod
...

I tried using buildkit but it has the same behaviour

08:37:20  #17 exporting to image
08:37:20  #17 exporting layers
08:50:12  #17 exporting layers 766.8s done

I also added node_modules to .dockerignore file but with no change.

Docker version 19.03.6, build 369ce74a3c

What could be the problem?

What is happening between "added 1483 packages..." and "Removing intermediate container"?

like image 890
kregi Avatar asked Oct 20 '25 04:10

kregi


1 Answers

I have found the cause of the problem and wanted to share if someone encounters similar issue.

The long "export layers" time was caused by node_modules folder that was present in one of the layers. Removing it solved the problem but it's really a workaround.

I merged npm ci and npm run build... steps together and removed node_modules in the same step.

RUN npm ci && npm run build:prod && rm -rf node_modules

So the final build stage in Dockerfile looks like this

FROM node:14.1.0-alpine AS build
COPY server/package*.json /nodejs/server/
WORKDIR /nodejs/server
RUN npm ci
COPY server ./

WORKDIR /nodejs
COPY package*.json ./
COPY *.json ./
COPY src ./src/
RUN npm ci && npm run build:prod && rm -rf node_modules
...

As I said it's just a workaround and I think the root cause is that Docker is having problems with exporting layers with a lot of small files such as the ones in node_modules.

like image 56
kregi Avatar answered Oct 22 '25 18:10

kregi