调整用于构建与 Task 兼容的自定义镜像的 Containerfile

特性概述

在 Tekton 中,为了增强安全性,Task 可能会配置 runAsNonRoot: true,这要求容器以非 root 用户运行。因此,在构建自定义镜像时,需要特别注意 Containerfile 的配置,以确保镜像能够在此类 Task 中正确运行。

本文档介绍如何调整 Containerfile,以构建与 Task 兼容的自定义镜像,重点关注用户权限配置。

使用场景

以下场景需要参考本文档中的指导:

  • 为在 Task 中使用而构建自定义镜像
  • 在 Task 中运行现有镜像时遇到与权限相关的错误
  • 确保镜像满足 Task 的安全要求

前提条件

在使用此特性之前,请确保:

  • 你拥有一个 OCI 镜像构建环境
    • 可以使用平台原生的构建流水线
    • 如果需要使用 Community/开源工具,请确保具备互联网访问能力,或已准备好离线包
  • 你对 Containerfile 编写有基本了解
  • 已准备好 Containerfile 文件及相关配置

步骤

1. 确认基础镜像

首先,确认基础镜像的发布版本,因为创建用户的命令可能会因版本不同而有所差异:

# Check the release version of the base image
$ podman run -it --rm ${registry} cat /etc/os-release

# Possible outputs
NAME="Alpine Linux"
# or
NAME="Debian GNU/Linux"
# or
NAME="Ubuntu"
# or
NAME="CentOS Linux"

2. 添加非 root 用户

在 Containerfile 中添加一个非 root 用户(建议使用 UID 65532):

# Based on the base image, choose the corresponding command

# Alpine
RUN adduser -u 65532 -h /home/nonroot -D nonroot

# Debian
RUN adduser --home /home/nonroot --uid 65532 nonroot --disabled-password --gecos ""

# Ubuntu
RUN apt-get update && apt-get install -y adduser \
    && adduser --home /home/nonroot --uid 65532 nonroot --disabled-password --gecos ""

# CentOS
RUN groupadd -g 65532 nonroot && useradd -u 65532 -U -d /home/nonroot -m nonroot

3. 为用户设置必要权限(可选)

如果该用户需要访问某些目录或文件,应为该用户添加必要的权限。

# Set the owner of the directory or file to nonroot
RUN chown -R nonroot:nonroot /path/to/directory

# Alternatively, set the permissions of the directory or file to allow everyone to read and write or to other minimal permissions
RUN chmod -R a+rwx /path/to/directory

4. 设置默认用户

在 Containerfile 中设置默认用户(使用 UID,而不是用户名):

由于配置了 runAsNonRoot 的 Pod 会检查用户 ID 是否为非 root 用户,而不是检查用户名。

# Set the default user to nonroot (using UID)
USER 65532

5. 验证镜像

构建完成后,验证镜像是否可以正常运行:

# Verify user configuration
$ podman run -it --rm ${registry} id

# Expected output
uid=65532(nonroot) gid=65532(nonroot) groups=65532(nonroot)

# Verify application permissions
$ podman run -it --rm ${registry} ls -la /home/nonroot

操作结果

采用此配置后:

  1. 用户配置

    • 始终使用 UID 65532,使多个 Task 中生成的文件能够具有一致的访问权限
    • 确保用户具有适当的工作目录权限
    • 避免使用 root 用户或 UID 0
  2. 应用配置

    • 确保应用可以作为非 root 用户正常运行
    • 在 Containerfile 中预先配置必要的目录权限
    • 使用 VOLUME 指令定义需要持久化的目录
  3. 安全建议

    • 定期更新基础镜像,以修复安全漏洞
    • 使用多阶段构建减少镜像大小
    • 在配置用户权限时遵循最小权限原则

故障排查

如果在 Task 中运行镜像时出现权限问题,可以:

  1. 查看 Pod 事件中的错误信息
  2. 验证镜像中的用户配置是否正确
  3. 确保已为应用配置所需权限

了解更多