DEBUG.WIKI
阿里云;从建站到云上业务;云服务器 · 数据库 · 对象存储;查看产品 →
PanstarCloud:部署应用,接入 AI,云服务器与 AI 网关
IPPeak:从目标地区访问网页,住宅代理、SEO 监测、广告验证

解决 K8s 报错 exec bash executable file not found 异常

分析在 Alpine 基础镜像容器中执行 bash 报 executable file not found in PATH 的 sh 替代与安装方案。

问题现象

Kubernetes Pod 启动陷入 CrashLoopBackOff,查看日志输出:

failed to create containerd task: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: exec: "bash": executable file not found in $PATH: unknown
# 或使用 kubectl exec 时:
OCI runtime exec failed: exec failed: unable to start container process: exec: "bash": executable file not found in $PATH: unknown

触发根因

  1. 容器镜像是基于精简的 Alpine Linux 构建的。Alpine 为了追求极小体积,默认只内置了轻量的 /bin/sh(基于 BusyBox),默认完全没有安装 bash。
  2. Pod 清单中 command 字段声明了 ["bash", "-c", "..."],因找不到 /bin/bash 而报错。

解决步骤与操作命令

步骤一:改用通用的 /bin/sh 启动

修改 Deployment 清单:

# 错误写法(在 Alpine 上失败)
command: ["bash", "-c", "echo ready && ./start.sh"]

# 正确写法(所有 Linux 镜像通用内置)
command: ["/bin/sh", "-c", "echo ready && ./start.sh"]

步骤二:使用 kubectl exec 时指定 /bin/sh

接入容器排查时不要写 bash:

kubectl exec -it <pod-name> -- /bin/sh

步骤三:若确实需要 bash,在 Dockerfile 中主动安装

FROM alpine:3.18
RUN apk add --no-cache bash

验证确认

更新 Deployment 后: Pod 顺畅调用 /bin/sh 执行启动流程,状态由 CrashLoopBackOff 顺利转为 Running。