Skip to content

Commit

Permalink
Modify rule S6597: Update wording for LaYC (#3139)
Browse files Browse the repository at this point in the history
  • Loading branch information
petertrr authored Sep 25, 2023
1 parent c8a38c9 commit 1e51830
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions rules/S6597/docker/rule.adoc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
== Why is this an issue?

In single Dockerfile instructions like `RUN`, `CMD`, and `ENTRYPOINT` multiple commands can be run, including the `cd` command for changing directories.
Using `WORKDIR` instead reduces the complexity of the above instructions and makes them easy to read, understand, troubleshoot, and maintain.
In Dockerfile, instructions `RUN`, `CMD`, and `ENTRYPOINT` can contain long shell scripts chaining multiple commands, including the `cd` command for changing directories.
Using `WORKDIR` instruction instead reduces the complexity of the above instructions and makes them easier to read, understand, troubleshoot, and maintain.

=== What is the potential impact?

Expand All @@ -16,9 +16,9 @@ RUN cd /tmp && \
cd /app/bin
----

In this example, the first `cd /tmp` command can be replaced by `WORKDIR cd /tmp` put above `RUN`.
The last `cd /app/bin` command can be replaced too.
The result will be following:
In this example, the first `cd /tmp` command can be replaced by `WORKDIR /tmp` before `RUN`.
The last `cd /app/bin` command can be replaced with `WORKDIR /app/bin` after the `RUN` instruction.
The result will be the following:

[source,docker]
----
Expand All @@ -32,19 +32,20 @@ WORKDIR /app/bin
Those actions will reduce the length of the `RUN` instruction, which makes it easier to read and understand.
Sometimes, it is hard to avoid the usage of `cd` command, especially in the middle of a long script.
Removing them from the beginning and end of a multi-line is an easy improvement.
Additionally, a lot of commands work well with absolute paths, so changing directories can be avoided in most cases.
Additionally, many commands work well with absolute paths, so changing directories can be avoided in most cases.

The `WORKDIR` instruction can be used multiple times in a Dockerfile.
It changes the current directory for the next instructions and until there is a following change.
This approach simplifies understanding of what is a current directory.

The same principles apply to `CMD` and `ENTRYPOINT` instructions.
Following this recommendation provides a clear structure for Dockerfiles, making it easier to maintain.

This recommendation provides a clear structure for Dockerfiles, making it easier to maintain.

== How to fix it

Where possible, ensure that all usages of `cd` commands are replaced by a `WORKDIR` instruction.
The `cd` commands at the beginning or end of multiline commands are a reliable sign, that they can be replaced.
The `cd` commands at the beginning or end of a chain of commands are a reliable sign that they can be replaced.
Also, using absolute paths can be considered for commands that accept them.

=== Code examples
Expand All @@ -60,8 +61,11 @@ RUN cd /app/bin && ./start.sh

[source,docker,diff-id=1,diff-type=compliant]
----
WORKDIR app/bin
WORKDIR /app/bin
RUN ./start.sh
# Or:
RUN /app/bin/start.sh
----

== Resources
Expand Down

0 comments on commit 1e51830

Please sign in to comment.