# 错误：容器设置了 runAsNonRoot，但镜像将以 root 用户运行

- **ID:** `policy/kubernetes-podsecuritypolicy-privileged-container`
- **领域:** policy
- **类别:** config_error
- **错误码:** `Forbidden`
- **验证级别:** ai_generated
- **修复率:** 85%

## 根因

Kubernetes PodSecurityPolicy 或 OPA Gatekeeper 强制要求容器不能以 root 运行，但容器镜像的默认用户是 root，且未设置 securityContext.runAsUser。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| Kubernetes v1.24+ | active | — | — |
| PodSecurity admission v1.25+ | active | — | — |
| OPA Gatekeeper v3.10+ | active | — | — |

## 解决方案

1. ```
   在 Pod spec 中设置 securityContext.runAsUser 为非 root 用户 ID（例如 1000），并确保镜像支持。
   ```
2. ```
   重建容器镜像，在 Dockerfile 中使用非 root USER 指令。
   ```

## 无效尝试

- **** — This violates the PodSecurityPolicy baseline/restricted profile, causing the admission controller to reject the Pod entirely. (70% 失败率)
- **** — Privileged containers are explicitly denied by restricted policies and also violate security best practices. (90% 失败率)
