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

解决 Kaniko 报错 failed to extract layer 只读文件系统修复

排查 Kubernetes 流水线运行 Google Kaniko 提示 error building image: failed to extract layer: read-only file system,检查容器安全上下文与挂载卷配置。

问题现象

在 GitLab CI 或 Tekton 流水线中使用 Kaniko 容器构建镜像时,解压基础层阶段报错崩溃:

error building image: error building stage: failed to execute command: 
failed to extract layer: open /usr/bin/tar: read-only file system

触发根因

Kaniko 在执行镜像解压与构建时,是直接在**其自身所在的当前容器根文件系统(Rootfs)**中解压覆盖文件的:

  1. Kubernetes Pod 开启了 securityContext.readOnlyRootFilesystem: true;
  2. 将整个根目录误挂载为了只读 ConfigMap 或只读卷。

解决步骤

1. 确保 Kaniko 容器的根文件系统为可写

在 Pod 或 Pipeline 的安全上下文规范中,确保关闭根文件系统只读保护:

spec:
  containers:
  - name: kaniko
    image: gcr.io/kaniko-project/executor:v1.20.0-debug
    securityContext:
      readOnlyRootFilesystem: false # 必须允许 Kaniko 写入其本地容器文件系统
      runAsUser: 0

2. 将临时构建缓存挂载到专用的 emptyDir 卷

若部分系统策略限制,将工作目录与缓存隔离至内存卷:

    volumeMounts:
    - name: kaniko-workspace
      mountPath: /workspace
  volumes:
  - name: kaniko-workspace
    emptyDir: {}

验证确认

重新触发 Kubernetes 流水线运行:

Kaniko 顺畅执行各 Dockerfile 指令,各镜像层成功推送到远程镜像仓库。