Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Speed up build by caching packages in Docker container

I have this idea that I haven't completed yet. In a Dockerfile I have:

FROM node:10

WORKDIR /app

RUN "*cache node modules here*"
RUN e.g. npm cache add foo bar baz

COPY package.json .
RUN npm i --cache-min 9999999 --loglevel=warn

COPY . .

CMD node dist

How can I create ultra high-performance Dockerfiles by caching some Node.js modules/package before the npm install step?

Is there some trick that can do this?

like image 885
Alexander Mills Avatar asked Sep 16 '25 10:09

Alexander Mills


1 Answers

This is out-of-the-box Docker functionality. If your Dockerfile says

FROM node:10
WORKDIR /app
COPY package.json .
RUN npm install

COPY ...

then, if the package.json hasn't changed, Docker will skip over the RUN npm install step and use the filesystem image that results from doing it.

like image 195
David Maze Avatar answered Sep 19 '25 07:09

David Maze