简体   繁体   English

Docker 构建 nodejs 应用程序需要很长时间

[英]Docker build takes long time for nodejs application

I am experiencing long build times for nodejs applications when building image with docker build command.使用 docker 构建命令构建映像时,nodejs 应用程序的构建时间很长。

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.构建 Java 映像时不会发生这种情况。

My Dockerfile我的 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我尝试使用 buildkit 但它具有相同的行为

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.我还将 node_modules 添加到 .dockerignore 文件中,但没有任何变化。

Docker version 19.03.6, build 369ce74a3c Docker 版本 19.03.6,构建 369ce74a3c

What could be the problem?可能是什么问题呢?

What is happening between "added 1483 packages..." and "Removing intermediate container"? “添加的 1483 包...”和“删除中间容器”之间发生了什么?

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.较长的“导出层”时间是由其中一层中存在的 node_modules 文件夹引起的。 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.我将 npm ci 和 npm run build... 步骤合并在一起,并在同一步骤中删除了 node_modules。

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

So the final build stage in Dockerfile looks like this所以 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 ./
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.正如我所说,这只是一种解决方法,我认为根本原因是 Docker 在导出包含大量小文件(例如 node_modules 中的文件)的层时遇到问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM