Skip to content

Commit

Permalink
SONARIAC-1856 Modify S7019: add EXEC alternatives and exceptions (#4597)
Browse files Browse the repository at this point in the history
* SONARIAC-1856 Update S7019 content

* Remove script example

* Fix id

* Update rules/S7019/docker/rule.adoc

Co-authored-by: Jonas Wielage <jonas.wielage@sonarsource.com>

* Address review comment

---------

Co-authored-by: Jonas Wielage <jonas.wielage@sonarsource.com>
  • Loading branch information
1 parent efd18e5 commit 444c238
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions rules/S7019/docker/rule.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@ This can cause problems when trying to gracefully stop containers because the ma
Moreover, the exec form provides more control and predictability over the execution of the command.
It does not invoke a command shell, which means it does not have the potential side effects of shell processing.


=== Exceptions

The exec form does not allow shell features such as variable expansion, piping (`|`) and command chaining (`&&`, `||`, `;`).
In case you need to use these features, there are a few alternatives:
* Creation of a wrapper script
* Explicitly specify the shell to use with the `SHELL` instruction before `CMD` or `ENTRYPOINT`


This rule will not raise an issue if the `SHELL` instruction is used before the `CMD` or `ENTRYPOINT` instruction, as we consider this a conscious decision.

== How to fix it

=== Code examples
Expand All @@ -22,6 +33,14 @@ FROM scratch
ENTRYPOINT echo "Welcome!"
----

[source,docker,diff-id=2,diff-type=noncompliant]
----
FROM scratch
ENTRYPOINT echo "Long script with chaining commands" \
&& echo "Welcome!" \
&& echo "Goodbye"
----

==== Compliant solution

[source,docker,diff-id=1,diff-type=compliant]
Expand All @@ -30,6 +49,21 @@ FROM scratch
ENTRYPOINT ["echo", "Welcome!"]
----

[source,docker,diff-id=2,diff-type=compliant]
----
FROM scratch
SHELL ["/bin/bash", "-c"]
ENTRYPOINT echo "Long script with chaining commands" \
&& echo "Welcome!" \
&& echo "Goodbye"
----

[source,docker,diff-id=2,diff-type=compliant]
----
FROM scratch
ENTRYPOINT ["/entrypoint.sh"]
----

== Resources
=== Documentation

Expand Down

0 comments on commit 444c238

Please sign in to comment.