如何解决GitLab CI中timeout无法执行/bin/bash的报错?
GitLab CI 修复
timeout 无法找到 bash 的错误 问题场景
你在 .gitlab-ci.yml 中定义了如下 YAML 锚点:
.docker_daemon_to_start: &docker_daemon_to_start - timeout 300 /bin/bash -c -- 'while ! docker info; do sleep 1; done'
尝试替换为 /usr/bin/bash 后,流水线仍报错:
timeout: can't execute '/bin/bash': No such file or directory
修复方案
1. 确认镜像中 bash 的真实路径
先在流水线中添加临时步骤,排查当前 CI 镜像里 bash 的实际位置:
check_bash_path: script: - which bash - whereis bash
运行该步骤后,将得到的真实路径替换到锚点命令中即可。
2. 改用 sh 替代 bash
很多轻量镜像(如 Alpine)默认只预装 sh 而非 bash,直接替换 shell 类型即可:
.docker_daemon_to_start: &docker_daemon_to_start - timeout 300 /bin/sh -c -- 'while ! docker info; do sleep 1; done'
若镜像中 sh 的路径为 /usr/bin/sh,对应替换即可。
3. 简化 timeout 命令格式
无需指定 shell 绝对路径,让系统自动查找默认 shell:
.docker_daemon_to_start: &docker_daemon_to_start - timeout 300 bash -c 'while ! docker info; do sleep 1; done'
或者针对 sh:
.docker_daemon_to_start: &docker_daemon_to_start - timeout 300 sh -c 'while ! docker info; do sleep 1; done'
4. 为镜像安装 bash(若必须使用 bash)
如果脚本依赖 bash 特性,可在镜像中安装 bash:
- 针对 Alpine 镜像:
before_script: - apk add --no-cache bash
- 针对 Debian/Ubuntu 镜像:
before_script: - apt-get update && apt-get install -y bash
内容的提问来源于stack exchange,提问作者Shanmukh Borole
相关产品推荐
相关产品推荐

