DEV Community

Cover image for The fastest docker build in the old west 🤠
Marcos Henrique
Marcos Henrique

Posted on

The fastest docker build in the old west 🤠

Most of the Dockerfiles I see around are similar to the example below an api node, where we have the source being added to the container before installing the dependencies, this ends up slowing down the docker build by not taking advantage of the layer cache, because the docker build, when it identifies a change in one of the layers, redoes all the steps below again

FROM node:latest

WORKDIR /app

ADD . .

RUN npm install

CMD ["run", "start"]
Enter fullscreen mode Exit fullscreen mode

Western-style trigger 🧐


In this approach using node I am first passing the dependency file which in the case of this example is package.json, but is not limited to this, it can be any file to be done after the build and any change in the source code would not affect the dependency installation cache

FROM node:latest

WORKDIR /app

ADD package*.json ./

RUN npm install

ADD . .

ENTRYPOINT [ "npm" ]

CMD ["run", "start"]
Enter fullscreen mode Exit fullscreen mode

Oldest comments (1)

Collapse
 
sirseanofloxley profile image
Sean Allin Newell

How does the ADD . . know not to pull in the package json+lock file from the prior step?