[英]How do I run a Bash script in an Alpine Docker container?
我有一個目錄只包含兩個文件, Dockerfile
和sayhello.sh
:
.
├── Dockerfile
└── sayhello.sh
Dockerfile
讀取
FROM alpine
COPY sayhello.sh sayhello.sh
CMD ["sayhello.sh"]
和sayhello.sh
包含
echo hello
Dockerfile
構建成功:
kurtpeek@Sophiemaries-MacBook-Pro ~/d/s/trybash> docker build --tag trybash .
Sending build context to Docker daemon 3.072 kB
Step 1/3 : FROM alpine
---> 665ffb03bfae
Step 2/3 : COPY sayhello.sh sayhello.sh
---> Using cache
---> fe41f2497715
Step 3/3 : CMD sayhello.sh
---> Using cache
---> dfcc26c78541
Successfully built dfcc26c78541
但是,如果我嘗試run
它,我會executable file not found in $PATH
錯誤中executable file not found in $PATH
一個executable file not found in $PATH
的executable file not found in $PATH
:
kurtpeek@Sophiemaries-MacBook-Pro ~/d/s/trybash> docker run trybash
container_linux.go:247: starting container process caused "exec: \"sayhello.sh\": executable file not found in $PATH"
docker: Error response from daemon: oci runtime error: container_linux.go:247: starting container process caused "exec: \"sayhello.sh\": executable file not found in $PATH".
ERRO[0001] error getting events from daemon: net/http: request canceled
這是什么原因造成的? 我記得以類似的方式在基於debian:jessie
的圖像中運行腳本。 所以也許它是阿爾卑斯山特有的?
Alpine 使用ash作為默認 shell 而不是bash
。
所以你可以
將 /bin/bash 定義為你 sayhello.sh 的第一行,所以你的文件 sayhello.sh 將以 bin/sh 開頭
#!/bin/sh
在您的 Alpine 映像中安裝 Bash,正如您似乎期望 Bash 存在一樣,在您的 Dockerfile 中有這樣一行:
RUN apk add --no-cache --upgrade bash
這個答案是完全正確的並且工作正常。
還有另一種方法。 您可以在基於 Alpine 的 Docker 容器中運行 Bash 腳本。
您需要像下面這樣更改 CMD:
CMD ["sh", "sayhello.sh"]
這也有效。
請記住為所有腳本授予執行權限。
FROM alpine
COPY sayhello.sh /sayhello.sh
RUN chmod +x /sayhello.sh
CMD ["/sayhello.sh"]
通過使用CMD
,Docker 正在PATH
搜索sayhello.sh
文件,但是您將它復制到了/
,而該文件不在PATH
。
因此,請使用您要執行的腳本的絕對路徑:
CMD ["/sayhello.sh"]
順便說一句,正如@user2915097 所說,小心 Alpine 默認沒有 Bash,以防您的腳本在 shebang 中使用它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.