如何用Mate Engine打造免费桌面虚拟伙伴:从入门到精通
2026/6/17 16:19:58
rpm--root /mnt/rootfs -e --nodeps setup这条命令的作用是在指定的根目录中卸载 setup 包,并忽略依赖关系检查。
| 方面 | 说明 |
|---|---|
| setup 包的作用 | 提供基础系统配置文件(/etc/passwd,/etc/group,/etc/profile等) |
| 为什么删除 | 在容器镜像中不需要系统初始化脚本,这些文件占用空间且无用 |
| –nodeps 的意义 | 跳过依赖检查,因为其他包可能依赖 setup,但在最小化镜像中不关心 |
| 镜像优化 | 删除不必要的包,使镜像尽可能小 |
UBI 是 Universal Base Image,Red Hat 官方提供的容器基础镜像。让我详细解释。
UBI 9 = Universal Base Image 9 ↓ 基于 RHEL 9(Red Hat Enterprise Linux 9)包含:
✅ glibc(标准 C 库)
✅ SSL/TLS 证书
✅ 时区数据库
✅ 本地化文件
# 标准版(包含包管理器)FROM registry.redhat.io/ubi9/ubi:latest# 最小化版(推荐用于多阶段构建)FROM registry.redhat.io/ubi9/ubi-minimal:latest# 超最小化版(极度简化)FROM registry.redhat.io/ubi9/ubi-micro:latest| 版本 | 大小 | 包管理器 | 场景 |
|---|---|---|---|
| ubi:latest | 70-80 MB | dnf + rpm | 开发、需要灵活性 |
| ubi-minimal | 40-50 MB | microdnf(轻量版) | 生产、多阶段构建 |
| ubi-micro | 20-30 MB | 无包管理器 | 超轻量、只有运行时 |
# 第一阶段:使用标准 UBI 9 构建 FROM registry.redhat.io/ubi9/ubi:latest AS builder # 在这里可以自由安装任何包 RUN dnf install -y \ curl \ nginx \ openssl \ ca-certificates # 准备文件 RUN mkdir -p /mnt/rootfs && \ dnf install --installroot /mnt/rootfs \ curl nginx \ --releasever 9 \ --setopt install_weak_deps=false \ --nodocs -y && \ dnf --installroot /mnt/rootfs clean all && \ rpm --root /mnt/rootfs -e --nodeps setup # 第二阶段:使用最小化 UBI 9 运行 FROM registry.redhat.io/ubi9/ubi-minimal:latest # 只复制必需文件,“只复制必需文件”容易出问题,尤其是库路径、符号链接、权限和特殊文件(device、管道、selinux 标签)会丢失或错位 COPY --from=builder /mnt/rootfs/usr/bin/curl /usr/bin/curl COPY --from=builder /mnt/rootfs/usr/sbin/nginx /usr/sbin/nginx COPY --from=builder /mnt/rootfs/usr/lib64/ /usr/lib64/ COPY --from=builder /mnt/rootfs/etc/nginx/ /etc/nginx/ CMD ["/usr/sbin/nginx","-g","daemon off;"]注意:
“只复制必需文件”确实容易出问题,尤其是库路径、符号链接、权限和特殊文件(device、管道、selinux 标签)会丢失或错位。
应该改成:
COPY --from=builder /mnt/rootfs //mnt/rootfs 中的所有内容都被递归复制到目标 / 中
但是这种方式,也会存在潜在的风险(残留 rpm/dnf 元数据可能误导后续运维;重复或不一致的库导致难排查、覆盖系统配置:/etc 中的配置;符号链接与版本冲突:不同版本的 .so 文件与链接可能破坏依赖解析 等),
我们需要做到以下几点:
rpm --root /mnt/rootfs -e --nodeps setupdnf --installroot /mnt/rootfs clean all尤其3,我觉得最重要,尤其你使用COPY --from=builder /mnt/rootfs /这种覆盖方式!