作者:秋梯田那路77 | 来源:互联网 | 2023-01-31 05:20
我有一个只包含两个文件的目录,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
错误:
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的)?
1> user2915097..:
Alpine带有/ bin/sh作为默认shell而不是/ bin/bash
所以你可以
1)你的sayhello.sh的第一行是一个shebang定义/ bin/sh,所以你的文件sayhello.sh将以
bash
2)在您的Alpine图像中安装bash,因为您似乎期望bash存在,在Dockerfile中有这样一行
bash
2> aerokite..:
这个答案完全正确,工作正常.
还有另一种方法,你可以在基于alpine的docker中运行bash脚本
您需要像下面那样更改CMD
CMD ["sh", "sayhello.sh"]
这也有效.
3> 小智..:
请记住为所有脚本授予执行权限.
FROM alpine
COPY sayhello.sh /sayhello.sh
RUN chmod +x /sayhello.sh
CMD ["/sayhello.sh"]
如果Dockerfile中已有权限,则不需要授予执行权限