AdSense

網頁

2023/2/6

Docker Dockerfile指令 WORKDIR 作用

Docker Dockerfile指令WORKDIR用來指定容器的工作目錄,即後續的指令將會以WORKDIR指定的目錄為執行目錄。


例如下面的Dockerfile設定WORKDIR /app,則後續的RUN pwd執行時是在容器中的/app路徑執行。

Dockerfile

FROM alpine
WORKDIR /app
RUN pwd
WORKDIR cmd
RUN pwd

對此Dockerfile執行docker build -t test . --progress=plain --no-cache輸出如下,可以看到WORKDIR後的RUN pwd輸出分別為/app/app/cmd

% docker build -t test . --progress=plain --no-cache
...
#9 [1/5] FROM docker.io/library/alpine@sha256:f271e74b17ced29b915d351685fd4644785c6d1559dd1f2d4189a5e851ef753a
#9 sha256:5a3eea7f0dd9f8f97486076c344776efa97be6241b4106b612083e7ab0516c1c
#9 DONE 0.0s

#5 [2/5] WORKDIR /app
#5 sha256:3de458818c63d8b57943e9a662337ef26e894ccc43fbedb0d15f4db56fbec684
#5 CACHED

#6 [3/5] RUN pwd
#6 sha256:a5af5f90f2f424983f30fb013e4c29afc6ba84de2593f20700b530f0c6d9b6ca
#6 0.185 /app
#6 DONE 0.2s

#7 [4/5] WORKDIR cmd
#7 sha256:e5beb8e5b2a74879845ea83feccdaa81afcc782567e590762ece44c3ae76ad0f
#7 DONE 0.0s

#8 [5/5] RUN pwd
#8 sha256:2b025592df26d4747aa0c0d62c0a2a26f21e299af42a93a23ece2b2bbf76cc55
#8 0.270 /app/cmd
#8 DONE 0.3s
...


沒有留言:

AdSense