7.7. 构建基于 UBI 的镜像
您可以使用 Buildah 工具从 Containerfile 创建基于 UBI 的 web 服务器容器。您必须禁用所有非 UBI yum 存储库,以确保您的镜像只包含可重新分发的红帽软件。
注意
对于 UBI 最小镜像,使用 microdnf 而不是 yum:运行 microdnf update -y && rm -rf /var/cache/yum 和 运行 microdnf install httpd -y && microdnf clean all 命令。
先决条件
-
container-tools模块已安装。
流程
创建
Containerfile:FROM registry.access.redhat.com/ubi8/ubi USER root LABEL maintainer="John Doe" # Update image RUN yum update --disablerepo=* --enablerepo=ubi-8-appstream-rpms --enablerepo=ubi-8-baseos-rpms -y && rm -rf /var/cache/yum RUN yum install --disablerepo=* --enablerepo=ubi-8-appstream-rpms --enablerepo=ubi-8-baseos-rpms httpd -y && rm -rf /var/cache/yum # Add default Web page and expose port RUN echo "The Web Server is Running" > /var/www/html/index.html EXPOSE 80 # Start the service CMD ["-D", "FOREGROUND"] ENTRYPOINT ["/usr/sbin/httpd"]构建容器镜像:
# buildah bud -t johndoe/webserver . STEP 1: FROM registry.access.redhat.com/ubi8/ubi:latest STEP 2: USER root STEP 3: LABEL maintainer="John Doe" STEP 4: RUN yum update --disablerepo=* --enablerepo=ubi-8-appstream-rpms --enablerepo=ubi-8-baseos-rpms -y ... Writing manifest to image destination Storing signatures --> f9874f27050 f9874f270500c255b950e751e53d37c6f8f6dba13425d42f30c2a8ef26b769f2
验证
运行 web 服务器:
# podman run -d --name=myweb -p 80:80 johndoe/webserver bbe98c71d18720d966e4567949888dc4fb86eec7d304e785d5177168a5965f64测试 Web 服务器:
# curl http://localhost/index.html The Web Server is Running