linux - 如何在 Alpine Linux 中使用 bash 脚本?

标签 linux bash docker alpine-linux

我有兴趣使用 wait-for-it使用基于 Alpine Linux 发行版的镜像,让 Docker-Compose 中的一项服务等待另一项服务。

但是,如果我克隆 wait-for-it 存储库并向其中添加以下 Dockerfile

FROM alpine
RUN apk --update add bash
COPY wait-for-it.sh wait-for-it.sh
CMD ["./wait-for-it.sh", "www.google.com:80"]

然后我使用 docker build --tag waitforit . 构建它,然后使用 docker run waitforit,但我收到此错误消息:

timeout: can't execute '15': No such file or directory
wait-for-it.sh: timeout occurred after waiting 15 seconds for www.google.com:80

相比之下,这是我在我的 (Ubuntu 16.04 LTS) 计算机上运行此命令时看到的:

wait-for-it.sh: waiting 15 seconds for www.google.com:80
wait-for-it.sh: www.google.com:80 is available after 0 seconds

bash 脚本似乎无法在 Alpine 容器中运行,因为它在我的 Ubuntu 本地计算机上运行。我该如何解决这个问题?

最佳答案

wait-for-it 脚本不兼容 sh,因此您需要将 bash 安装到您的 alpine 镜像中。

但是,我是否可以建议避免这样的“hacks”并改用 HEALTHCHECK

例如,让一项服务等待另一项服务正常运行看起来像这样:

services:
  my_service:
    image: something
    healthcheck:
      test: nc -z 3306

  other_service
  depends_on:
    my_service:
      condition: service_healthy

请注意,这仅适用于 v2 组合文件,不适用于 v3

关于linux - 如何在 Alpine Linux 中使用 bash 脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43890995/

相关文章:

java - Dockerizing 使用 oracle-ojdbc : How do you get the driver in there? 的 Maven 应用程序

linux - 在汇编程序中编写 linux 内核模块时的内存访问

linux - 插入静态链接的二进制文件

linux - Bash 脚本 - for 循环和 if-else

bash - 文件列表中带有awk的文件大小总和

Linux:将命令添加到 bashrc 中的另一个命令

linux - ldconfig 未加载 ld.so.conf

bash - 使 Web 服务器进程保持事件状态的终端应用程序

authentication - 在 docker 中运行的 GUI 应用程序的 X11 转发

docker - 连接到在远程服务器上的 docker 中运行的 jupyter notebook