Pattern: Use of multiple consecutive RUN
Issue: -
Each RUN
instruction will create a new layer in the resulting image. Therefore consolidating consecutive RUN
instructions will reduce the layer count (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.
Example of incorrect code:
RUN command1
RUN command2
Example of correct code:
RUN command1 \
&& command2
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.