Reputation: 9694
The docker file has various command like FROM, RUN, etc. Each of this command creates a layer (intermediary image).
During build, assuming the layer already exists, on what basis does docker decide whether to create a new layer or use an existing layer?
Upvotes: 1
Views: 869
Reputation: 159091
The docker build
caching system is pretty simple. For most commands, if the previous layer was cached, and there is a layer that runs the exact same command (RUN
, ENV
, CMD
, ...) then it reuses the cached layer, and repeats this check for the next command. For COPY
and ADD
commands the decision is based on a hash of the file contents.
This is detailed in Best practices for writing Dockerfiles in the Docker documentation.
Practically, there are a couple of things this means:
docker build --no-cache
, since if the Dockerfile or any involved files have changed, the cache will be automatically invalidated.npm install
, pip install
, bundle install
, ...) have a first step that only COPY
s the files that list the dependencies, then RUN whatever install
, then COPY
the rest of your application. This avoids invalidating the cache for the "install" step if only application code has changed.RUN apt-get update && apt-get install
in a single command. This avoids a problem where the URLs from the "update" step are cached, but the packages in the "install" step change, and the cached URLs are no longer valid.Upvotes: 2