Adjust Containerfile for Building Task-Compatible Custom Images
目录
Feature OverviewUse CasesPrerequisitesSteps1. Confirm the Base Image2. Add a Non-Root User3. Set Necessary Permissions for the User (Optional)4. Set the Default User5. Validate the ImageOperation ResultsTroubleshootingLearn MoreFeature Overview
在 Tekton 中,为了增强安全性,Tasks 可能会配置 runAsNonRoot: true,这要求容器以非 root 用户身份运行。因此,在构建自定义镜像时,需要特别注意 Containerfile 的配置,以确保镜像能够在此类 Tasks 中正常运行。
本文档介绍如何调整 Containerfile,以构建兼容 Tasks 的自定义镜像,重点关注用户权限配置。
Use Cases
以下场景需要参考本文档的指导:
- 构建用于 Tasks 的自定义镜像
- 在 Tasks 中运行现有镜像时遇到权限相关错误
- 确保镜像满足 Tasks 的安全要求
Prerequisites
使用此功能前,请确保:
- 具备 OCI 镜像构建环境
- 可以使用平台的原生构建流水线
- 若需使用 community/开源工具,确保具备网络访问或已准备好离线包
- 具备 Containerfile 编写的基础知识
- 可参考 Dockerfile 官方文档
- 理解 Dockerfile 最佳实践
- 拥有 Containerfile 文件及相关配置
Steps
1. Confirm the Base Image
首先,确认基础镜像的发行版本,因为不同版本创建用户的命令可能不同:
2. Add a Non-Root User
在 Containerfile 中添加非 root 用户(建议使用 UID 65532):
3. Set Necessary Permissions for the User (Optional)
如果用户需要访问某些目录或文件,应为该用户添加必要的权限。
4. Set the Default User
在 Containerfile 中设置默认用户(使用 UID 而非用户名):
由于配置了
runAsNonRoot的 Pod 会检查用户 ID 是否为非 root 用户,而非用户名。
5. Validate the Image
构建完成后,验证镜像是否能正常运行:
Operation Results
通过此配置:
-
用户配置
- 统一使用 UID 65532,确保多个 Tasks 生成的文件具有一致的访问权限
- 确保用户拥有适当的工作目录权限
- 避免使用 root 用户或 UID 0
-
应用配置
- 确保应用能以非 root 用户正常运行
- 在 Containerfile 中预先配置必要的目录权限
- 使用
VOLUME指令定义需要持久化的目录
-
安全建议
- 定期更新基础镜像以修复安全漏洞
- 使用多阶段构建以减小镜像体积
- 配置用户权限时遵循最小权限原则
Troubleshooting
若在 Tasks 中运行镜像时出现权限问题,可以:
- 检查 Pod 事件中的错误信息
- 验证镜像中的用户配置是否正确
- 确认应用所需权限是否已配置