1. 程式人生 > 實用技巧 >【Docker】Dockerfile 之 WORKDIR

【Docker】Dockerfile 之 WORKDIR

參考教程:https://docs.docker.com/engine/reference/builder/

環境

  1. virtual box 6.1
  2. centos 7.8
  3. docker 19.03

WORKDIR

WORKDIR /path/to/workdir

The WORKDIR instruction sets the working directory for any RUN, CMD, ENTRYPOINT, COPY and ADD instructions that follow it in the Dockerfile. If the WORKDIR doesn’t exist, it will be created even if it’s not used in any subsequent Dockerfile

instruction.

WORKDIR 指令為 Dockerfile 中跟在其後的所有 RUNCMDENTRYPOINTCOPYADD 指令設定工作目錄。如果 WORKDIR 不存在,即使之後的 Dockerfile 指令中未使用它也將被建立。

The WORKDIR instruction can be used multiple times in a Dockerfile. If a relative path is provided, it will be relative to the path of the previous WORKDIR instruction. For example:

WORKDIR 指令可在 Dockerfile 中多次使用。如果提供了相對路徑,則它將相對於上一個 WORKDIR 指令的路徑。 例如:

WORKDIR /a
WORKDIR b
WORKDIR c
RUN pwd

The output of the final pwd command in this Dockerfile would be /a/b/c.
Dockerfile 中最後一個 pwd 命令的輸出為 /a/b/c

The WORKDIR instruction can resolve environment variables previously set using ENV. You can only use environment variables explicitly set in the Dockerfile

. For example:

WORKDIR 指令可以解析以前使用 ENV 設定的環境變數。您只能使用在 Dockerfile 中顯式設定的環境變數。 例如:

ENV DIRPATH=/path
WORKDIR $DIRPATH/$DIRNAME
RUN pwd

The output of the final pwd command in this Dockerfile would be /path/$DIRNAME.

Dockerfile 中最後一個 pwd 命令的輸出為 /path/$DIRNAME

總結

介紹了 Dockerfile 中 WORKDIR 指令的用法和注意事項。