Bitbucket Pipeline无法正确加载Ubuntu镜像,执行命令报错
问题排查与解决
错误原因分析
出现bash: dircolors: command not found和bash: apt-get: command not found,核心原因是实际运行构建的容器并非你指定的ubuntu:latest镜像,大概率是以下两种情况:
- Bitbucket仓库的Pipelines全局设置中指定了默认镜像,覆盖了yml文件里的配置
bitbucket-pipelines.yml存在语法问题,导致镜像配置未被正确加载,系统自动使用了Bitbucket Pipelines的默认镜像(通常是基于Alpine Linux的atlassian/default-image,这类镜像用apk而非apt-get,且没有dircolors工具)
排查步骤
- 验证当前运行的系统:在script最开头添加以下命令,确认实际使用的镜像系统:
cat /etc/os-release which apt-get || which apk
- 检查仓库Pipelines设置:登录Bitbucket仓库,进入「Settings > Pipelines > Settings」,查看「Default Docker image」是否被修改为非Ubuntu镜像,若有则要么改回默认,要么在yml里明确指定镜像覆盖全局设置。
- 检查yml语法:确认
image: ubuntu:latest是顶格写(和pipelines同层级),无拼写错误(比如写成images),可使用Bitbucket的「Pipelines > Validate」功能验证yml语法正确性。
修正后的配置示例
方案1:确保使用Ubuntu镜像
如果坚持用Ubuntu镜像,需保证配置正确,同时提前安装dircolors所属的coreutils包:
image: ubuntu:latest pipelines: branches: master: - step: name: Upload script: # 先安装必要工具,包含dircolors所属的coreutils - apt-get update && apt-get install -y openssh-client jq curl coreutils - cat /opt/atlassian/pipelines/agent/ssh/id_rsa >> ec2_private_key.pem - chmod 600 ec2_private_key.pem - echo "Fetching modified files from Bitbucket API using diff endpoint" - | curl -s -H "Authorization: Bearer $API_TOKEN_BEARER" \ "https://api.bitbucket.org/2.0/repositories/$BITBUCKET_REPO_FULL_NAME/commits/master" \ | jq -r '.values[0].hash' > latest_commit.txt - COMMIT_HASH=$(cat latest_commit.txt) - | curl -s -H "Authorization: Bearer $API_TOKEN_BEARER" \ "https://api.bitbucket.org/2.0/repositories/$BITBUCKET_REPO_FULL_NAME/diff/$COMMIT_HASH" \ | grep '^+++ b' | awk '{print $2}' | sed 's/^b\///' > modified_files.txt - echo "Modified files:" - cat modified_files.txt - while IFS= read -r file; do scp -i ec2_private_key.pem -o StrictHostKeyChecking=no -r "$file" ubuntu@$EC2_HOST:$PATH"$file"; done < modified_files.txt - ssh -i ec2_private_key.pem -o StrictHostKeyChecking=no ubuntu@$EC2_HOST "cd $PATH && sudo docker-compose up --build -d"
方案2:改用Bitbucket默认Alpine镜像
如果打算使用Bitbucket的默认镜像,需将apt-get命令替换为apk:
image: atlassian/default-image:3 pipelines: branches: master: - step: name: Upload script: - apk update && apk add openssh-client jq curl coreutils - cat /opt/atlassian/pipelines/agent/ssh/id_rsa >> ec2_private_key.pem - chmod 600 ec2_private_key.pem # 后续脚本内容与原配置一致
内容的提问来源于stack exchange,提问作者Some d
相关产品推荐
相关产品推荐

