variable
variable

Reputation: 9694

During build, on what basis does docker decide whether to create a new layer or use an existing layer?

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

Answers (1)

David Maze
David Maze

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:

  • You almost never need docker build --no-cache, since if the Dockerfile or any involved files have changed, the cache will be automatically invalidated.
  • If you have an expensive step to install dependencies (npm install, pip install, bundle install, ...) have a first step that only COPYs 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.
  • If you have a Debian- or Ubuntu-based image, 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

Related Questions