Caching Issues

Our Dockerfile has a slight issue with how the caching works. In this lesson, we will discuss these caching issues.

Caching issue 1: updating packages

Currently, our Dockerfile has the following two lines:

RUN apt-get update -yqq 
RUN apt-get install -yqq --no-install-recommends nodejs 

Although this works, there is a hidden problem. Let’s say we realize at a later stage that we need to install an extra package, for example, the Vim editor. We add the vim package to the apt-get install RUN instruction, busting the cache and causing that instruction to rerun:

RUN apt-get update -yqq 
RUN apt-get install -yqq --no-install-recommends nodejs vim 

However, the apt-get update RUN instruction remains unchanged, and the cached repository details are used. Rather than getting the current, latest version of the new package we have added, we will be getting whatever was the latest at the time we last built our image. That behavior is almost never what we want.

For that reason, it is recommended to always combine the apt-get update and apt-get install commands into a single RUN instruction like this:

RUN apt-get update -yqq && apt-get install -yqq --no-install-recommends nodejs vim 

This ensures that whenever you change the packages being installed, you will also get the latest repository information at the same time.

Finally, it is good practice to format the apt-get install command as follows:

RUN apt-get update -yqq && apt-get install -yqq --no-install-recommends \
nodejs \
vim

Using one package per line and keeping packages in alphabetical order make it easier to see which packages are installed and locate ones that need to be changed if you have many packages installed.

Let’s fix this issue in our Dockerfile now. We do not need vim installed currently, so our two RUN instructions for apt-get update and apt-get install will become:

RUN apt-get update -yqq && apt-get install -yqq --no-install-recommends nodejs 

Get hands-on with 1400+ tech skills courses.