![Docker WORKDIR 混亂](https://rvso.com/image/756267/Docker%20WORKDIR%20%E6%B7%B7%E4%BA%82.png)
我是 Docker 的初學者,我有一個非常基本的 Dockerfile 用於練習目的。
FROM debian:latest
WORKDIR /init
COPY hello_world.sh .
RUN hello_world.sh
CMD [ "/bin/bash" ]
在我嘗試建立圖像後,我得到了:
Sending build context to Docker daemon 3.072kB
Step 1/5 : FROM debian:latest
---> ee11c54e6bb7
Step 2/5 : WORKDIR /init
---> Using cache
---> 605ee1ff67e9
Step 3/5 : COPY hello_world.sh .
---> Using cache
---> f544ee4e93b8
Step 4/5 : RUN hello_world.sh
---> Running in c4cbd6389bc2
/bin/sh: 1: hello_world.sh: not found
The command '/bin/sh -c hello_world.sh' returned a non-zero code: 127
如果我修改 RUN 指令並設定絕對路徑/init/hello_world.sh,效果很好。根據描述,上面的 Dockerfile 應該可以工作。
來自 Dockerfile 參考:
The WORKDIR instruction sets the working directory for any RUN, CMD, ENTRYPOINT, COPY and ADD instructions that follow it in the Dockerfile.
我誤會了什麼?
答案1
嘗試RUN ./hello_world.sh
。事實是工作目錄由 RUN 偵探使用,並不意味著自動出現在 PATH 中。這還只是工作目錄執行進程的地方。