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

功能概述

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

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

使用场景

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

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

前提条件

在使用此功能之前,请确保:

  • 你拥有一个 OCI 镜像构建环境
    • 你可以使用平台的原生构建流水线
    • 如果你需要使用社区/开源工具,请确保你可以访问互联网,或者已经准备好离线安装包
  • 你对 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 的 Pods 会检查用户 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,这样多个 Tasks 中生成的文件可以具有一致的访问权限
    • 确保用户具有适当的工作目录权限
    • 避免使用 root 用户或 UID 0
  2. 应用配置

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

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

故障排查

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

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

了解更多