改一行 JS 却重跑 74 秒 install,先问缓存断在哪层

docker history 是这张便签的第一步,先看层在哪断的。 机器 CentOS 7.9、Docker 20.10.7。

$ docker history shop-gateway:1.4 --no-trunc

想知道哪层最肥、命令被截断时,就翻这条。 层 id 全显示 <missing> 是上回 prune 过,中间镜像被清掉只剩大小。

改 Dockerfile 只调个顺序,先拷两份 json 再装:

COPY admin/package.json admin/package-lock.json ./
RUN npm ci
COPY admin/ .

install 层想 CACHED 住,上面只能跟着那两份 json。 中间插全量 COPY 就白拆。

反向那个坑更阴,提醒自己记这儿:

npm ERR! code EUSAGE
npm ERR! npm ci can only install packages when your package.json and package-lock.json are in sync.

加了包没动 package.json,那层还 Using cache。 依赖真变了的时候,CACHED 是个错误信号,装进去的还是老 lock 那套。

拆完 74 秒没了,整趟从 9 分 12 秒掉到 7 分钟出头。

镜像能稳定出了,可 exec 进去调试那步又栽了,回头那张讲。

posted @ 2026-10-30 14:10  kaka的程序人生  阅读(3)  评论(0)    收藏  举报