如何在 Alpine Docker 容器中运行 Bash 脚本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44803982/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I run a Bash script in an Alpine Docker container?
提问by Kurt Peek
I have a directory containing only two files, Dockerfile
and sayhello.sh
:
我有一个只包含两个文件的目录,Dockerfile
并且sayhello.sh
:
.
├── Dockerfile
└── sayhello.sh
The Dockerfile
reads
在Dockerfile
读
FROM alpine
COPY sayhello.sh sayhello.sh
CMD ["sayhello.sh"]
and sayhello.sh
contains simply
并sayhello.sh
简单地包含
echo hello
The Dockerfile
builds successfully:
在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
However, if I try to run
it I get an executable file not found in $PATH
error:
但是,如果我尝试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
What is causing this? (I recall running scripts in debian:jessie
-based images in a similar manner, so perhaps it is Alpine-specific)?
这是什么原因造成的?(我记得debian:jessie
以类似的方式在基于 -based 的图像中运行脚本,所以它可能是 Alpine 特有的)?
回答by user2915097
Alpine comes with ashas the default shell instead of bash
.
Alpine 使用ash作为默认 shell 而不是bash
.
So you can
这样你就可以
Have a shebang defining /bin/bash as the first line of your sayhello.sh, so your file sayhello.sh will begin with bin/sh
#!/bin/sh
Install Bash in your Alpine image, as you seem to expect Bash is present, with such a line in your Dockerfile:
RUN apk add --no-cache --upgrade bash
将 /bin/bash 定义为你 sayhello.sh 的第一行,所以你的文件 sayhello.sh 将以 bin/sh 开头
#!/bin/sh
在您的 Alpine 映像中安装 Bash,正如您似乎期望 Bash 存在一样,在您的 Dockerfile 中有这样一行:
RUN apk add --no-cache --upgrade bash
回答by Mir Shahriar Sabuj
This answeris completely right and works fine.
这个答案是完全正确的并且工作正常。
There is another way. You can run a Bash script in an Alpine-based Docker container.
还有另一种方法。您可以在基于 Alpine 的 Docker 容器中运行 Bash 脚本。
You need to change CMD like below:
您需要像下面这样更改 CMD:
CMD ["sh", "sayhello.sh"]
And this works too.
这也有效。
回答by debus
Remember to grant execution permission for all scripts.
请记住为所有脚本授予执行权限。
FROM alpine
COPY sayhello.sh /sayhello.sh
RUN chmod +x /sayhello.sh
CMD ["/sayhello.sh"]
回答by zigarn
By using the CMD
, Docker is searching the sayhello.sh
file in the PATH
, BUT you copied it in /
which is not in the PATH
.
通过使用CMD
,码头工人正在搜索sayhello.sh
的文件PATH
,但你复制它/
是不是在PATH
。
So use an absolute path to the script you want to execute:
因此,使用您要执行的脚本的绝对路径:
CMD ["/sayhello.sh"]
BTW, as @user2915097 said, be careful that Alpine doesn't have Bash by default in case of your script using it in the shebang.
顺便说一句,正如@user2915097 所说,小心 Alpine 默认没有 Bash,以防您的脚本在 shebang 中使用它。