如何在Alpine Docker容器中运行Bash脚本? [英] How do I run a Bash script in an Alpine Docker container?
问题描述
我有一个仅包含两个文件的目录, Dockerfile
和 sayhello.sh
:
I have a directory containing only two files, Dockerfile
and sayhello.sh
:
.
├── Dockerfile
└── sayhello.sh
The 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
但是,如果我尝试运行
,我得到在$ PATH中找不到的可执行文件 c错误:
However, if I try to run
it I get an executable file not found in $PATH
error:
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
的图像中运行脚本,所以也许是阿尔卑斯特定的)?
What is causing this? (I recall running scripts in debian:jessie
-based images in a similar manner, so perhaps it is Alpine-specific)?
推荐答案
Alpine随附了 ash 作为默认设置而不是 bash
。
Alpine comes with ash as the default shell instead of bash
.
因此您可以
-
具有一个将/ bin / bash定义为sayhello.sh的第一行的shebang,因此您的文件sayhello.sh将以bin / sh开头
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
在您的Alpine映像中安装Bash,就像您似乎期望的那样,在Dockerfile中这样一行:
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
这篇关于如何在Alpine Docker容器中运行Bash脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!