[英]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.