DEV Community

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

Posted on

4 1

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

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top 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?

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

AWS GenAI LIVE!

GenAI LIVE! is a dynamic live-streamed show exploring how AWS and our partners are helping organizations unlock real value with generative AI.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️