2023-04-17 15:05:40 +02:00
|
|
|
The `cd` command should not be used to change the working directory.
|
|
|
|
Instead `WORKDIR` should be used.
|
|
|
|
|
|
|
|
== Why is this an issue?
|
|
|
|
|
|
|
|
The `cd` commands in `RUN` (and other) instructions make them longer, harder to read, troubleshoot, and maintain.
|
|
|
|
It is better to use the `WORKDIR` instruction to set a working directory for the next Docker instructions.
|
|
|
|
|
|
|
|
== How to fix it
|
|
|
|
|
|
|
|
=== Code examples
|
|
|
|
|
|
|
|
==== Noncompliant code example
|
|
|
|
|
|
|
|
[source,docker,diff-id=1,diff-type=noncompliant]
|
|
|
|
----
|
|
|
|
RUN cd /app/bin && ./start.sh
|
|
|
|
----
|
|
|
|
|
|
|
|
==== Compliant solution
|
|
|
|
|
|
|
|
[source,docker,diff-id=1,diff-type=compliant]
|
|
|
|
----
|
|
|
|
WORKDIR app/bin
|
|
|
|
RUN ./start.sh
|
|
|
|
----
|
|
|
|
|
|
|
|
=== How does this work?
|
|
|
|
|
|
|
|
The `WORKDIR` instruction can be used multiple times in Dockerfile.
|
|
|
|
It sets the working directory for any `RUN`, `CMD`, `ENTRYPOINT`, `COPY`, and `ADD` instructions that follow.
|
|
|
|
It increases clarity and reliability.
|
|
|
|
|
|
|
|
|
|
|
|
== Resources
|
|
|
|
=== Documentation
|
2023-04-17 17:34:36 +02:00
|
|
|
|
|
|
|
* https://docs.docker.com/develop/develop-images/dockerfile_best-practices/#workdir[WORKDIR - Best practices for writing Dockerfiles]
|
|
|
|
* https://docs.docker.com/engine/reference/builder/#workdir[WORKDIR - Dockerfile reference]
|
2023-04-17 15:05:40 +02:00
|
|
|
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
|
|
== Implementation Specification
|
|
|
|
(visible only on this page)
|
|
|
|
|
|
|
|
=== Message
|
|
|
|
|
|
|
|
WORKDIR instruction should be used instead of cd command.
|
|
|
|
|
|
|
|
=== Highlighting
|
|
|
|
|
|
|
|
Highlight usage of cd command.
|
|
|
|
|
|
|
|
'''
|
|
|
|
endif::env-github,rspecator-view[]
|