Consider this example from the docs:
# syntax=docker/dockerfile:1 FROM node WORKDIR /app COPY package.json yarn.lock . # Copy package management files RUN npm install # Install dependencies COPY . . # Copy over project files RUN npm build # Run build
By installing dependencies in earlier layers of the Dockerfile, there is no need to rebuild those layers when a project file has changed.
Which layers can be skipped here?
By my understanding, npm
command is black-box for docker,
so docker doesn't know what npm
's inputs are and what will it produce.
If so, then docker has to always run npm install
and npm build
commands, which means caching is useless here.
What am I missing here?
No.
All.
Docker exactly knows that - the input is all the files and the image produced from earlier stages, and the output are all the modifications made to the files in the image. The modifications are saved as a new layer.