8000 DL3059 · hadolint/hadolint Wiki · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
Martin Jambon edited this page Sep 2, 2022 · 4 revisions

Multiple consecutive RUN instructions. Consider consolidation.

Problematic code:

RUN command_creating_big_files     # big files stored in image layer!
RUN command_deleting_these_files

or

RUN command_updating_package_info  # cached in image layer and not fresh!
RUN command_using_package_info

Correct code:

RUN command_creating_big_files \
 && command_deleting_these_files

or, respectively

RUN command_updating_package_info \
 && command_using_package_info

Rationale:

Each RUN instruction will create a new layer in the resulting image. Therefore consolidating consecutive RUN instructions will reduce the layer count. This will reduce the size of the image if it allows temporary files to be deleted in the same RUN instruction as the one that created them (see https://docs.docker.com/develop/dev-best-practices/). In addition to that, each RUN runs in its own shell, which can be the source of confusion when part of a RUN instruction changes something about the environment, because these changes may vanish in the next RUN instruction.

Drawbacks

Please review Docker's layer caching. In general you want layers first which don't change often, so they can be cached. Putting easy cacheable and non-cacheable commands in the same layer (by using one RUN command) might have negative performance issues. You might want to ignore this info level rule.

Clone this wiki locally
0