From 32c720335679b30d9042fdf788f45b8b8f1ad70d Mon Sep 17 00:00:00 2001 From: Rudy Regazzoni <110470341+rudy-regazzoni-sonarsource@users.noreply.github.com> Date: Fri, 6 Dec 2024 14:09:50 +0100 Subject: [PATCH 1/2] SONARIAC-1701 Update RSPEC description for S6587 --- rules/S6587/docker/rule.adoc | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/rules/S6587/docker/rule.adoc b/rules/S6587/docker/rule.adoc index 67b13a3b18a..e86b7528c4b 100644 --- a/rules/S6587/docker/rule.adoc +++ b/rules/S6587/docker/rule.adoc @@ -131,6 +131,21 @@ They are not removed by the `clean` command, so they need to be removed manually Alternatively, store the cache in a dedicated cache mount. A cache mount can be created by adding a flag `--mount type=cache` to the `RUN` command. + This will store the cache in a Docker volume, which will be persisted between builds making the build faster. +Also, each `RUN` instruction creates a new layer, and any changes made in one layer are not visible in the next. Thus, the cache should be removed in the same layer as the installation in the same `RUN` instruction. + +The following code incorrectly cleans the cache: +[source,docker] +---- +RUN apt-get install -y git +RUN apt-get clean && rm -rf /var/lib/apt/lists/* +---- +It should be written as: +[source,docker] +---- +RUN apt-get install -y git \ + && apt-get clean && rm -rf /var/lib/apt/lists/* +---- + == Resources === Documentation From b8a2d8ab6e506d3b7d9b0a31cabc5e45f918d4b0 Mon Sep 17 00:00:00 2001 From: Rudy Regazzoni <110470341+rudy-regazzoni-sonarsource@users.noreply.github.com> Date: Mon, 9 Dec 2024 16:49:05 +0100 Subject: [PATCH 2/2] Address review comments --- rules/S6587/docker/rule.adoc | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/rules/S6587/docker/rule.adoc b/rules/S6587/docker/rule.adoc index e86b7528c4b..4744a8adb5a 100644 --- a/rules/S6587/docker/rule.adoc +++ b/rules/S6587/docker/rule.adoc @@ -131,19 +131,18 @@ They are not removed by the `clean` command, so they need to be removed manually Alternatively, store the cache in a dedicated cache mount. A cache mount can be created by adding a flag `--mount type=cache` to the `RUN` command. + This will store the cache in a Docker volume, which will be persisted between builds making the build faster. -Also, each `RUN` instruction creates a new layer, and any changes made in one layer are not visible in the next. Thus, the cache should be removed in the same layer as the installation in the same `RUN` instruction. +Also, each `RUN` instruction creates a new layer, and any changes made in one layer are not visible in the next. Thus, the cache should be removed in the same layer (i.e., the same `RUN` instruction) as the installation. The following code incorrectly cleans the cache: [source,docker] ---- -RUN apt-get install -y git -RUN apt-get clean && rm -rf /var/lib/apt/lists/* +RUN apt-get install nginx +RUN apt-get clean ---- It should be written as: [source,docker] ---- -RUN apt-get install -y git \ - && apt-get clean && rm -rf /var/lib/apt/lists/* +RUN apt-get install nginx && apt-get clean ---- == Resources