如何在 Alpine Docker 容器中运行 Bash 脚本? [英] How do I run a Bash script in an Alpine Docker container?

查看:55
本文介绍了如何在 Alpine Docker 容器中运行 Bash 脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个只包含两个文件的目录,Dockerfilesayhello.sh:

<预><代码>.├── Dockerfile└── sayhello.sh

Dockerfile 读取

来自高山复制sayhello.sh sayhello.shCMD [sayhello.sh"]

sayhello.sh只包含

回声你好

Dockerfile 构建成功:

kurtpeek@Sophiemaries-MacBook-Pro ~/d/s/trybash>docker build --tag trybash .将构建上下文发送到 Docker 守护进程 3.072 kB第 1/3 步:来自高山--->第665话第 2/3 步:复制 sayhello.sh sayhello.sh--->使用缓存--->fe41f2497715步骤 3/3:CMD sayhello.sh--->使用缓存--->dfcc26c78541成功构建dfcc26c78541

但是,如果我尝试 run 它我得到一个 executable file not found in $PATH 错误:

kurtpeek@Sophiemaries-MacBook-Pro ~/d/s/trybash>码头工人运行trybashcontainer_linux.go:247:启动容器进程导致exec:"sayhello.sh":在$PATH 中找不到可执行文件";docker:来自守护进程的错误响应:oci 运行时错误:container_linux.go:247:启动容器进程导致exec:"sayhello.sh:在 $PATH 中找不到可执行文件".ERRO[0001] 从守护进程获取事件时出错:net/http:请求被取消

这是什么原因造成的?我记得以类似的方式在基于 debian:jessie 的图像中运行脚本.所以也许它是阿尔卑斯山特有的?

解决方案

Alpine 自带 ash作为默认 shell 而不是 bash.

所以你可以

  1. 将/bin/bash 定义为你 sayhello.sh 的第一行,所以你的文件 sayhello.sh 将以 bin/sh 开头

    #!/bin/sh

  2. 在您的 Alpine 映像中安装 Bash,正如您所期望的,Bash 存在,在您的 Dockerfile 中包含这样一行:

    RUN apk add --no-cache --upgrade bash

I have a directory containing only two files, Dockerfile and sayhello.sh:

.
├── Dockerfile
└── sayhello.sh

The Dockerfile reads

FROM alpine
COPY sayhello.sh sayhello.sh
CMD ["sayhello.sh"]

and sayhello.sh contains simply

echo hello

The Dockerfile builds successfully:

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:

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?

解决方案

Alpine comes with ash as the default shell instead of bash.

So you can

  1. 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
    

  2. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆