19.6. 使用 Buildah 从头开始创建镜像
您可以创建一个仅包含最小容器元数据的新容器,而不是从基础镜像开始。
从全新容器中创建镜像时,请考虑:
- 您可以将不带依赖项的可执行文件复制到涂销空间镜像中,再进行一些配置设置才能使容器正常工作。
-
您必须初始化 RPM 数据库,并在容器中添加一个发布软件包,以使用像
yum或rpm这样的工具。 - 如果您添加很多软件包,请考虑使用标准 UBI 或最小 UBI 镜像而不是 scratch 镜像。
先决条件
-
container-tools模块已安装。
流程
您可以将 Web 服务 httpd 添加到容器中,并将其配置为运行。
创建一个空容器:
# buildah from scratch working-container挂载
working-container容器,并将挂载点路径保存到scratchmnt变量中:# scratchmnt=$(buildah mount working-container) # echo $scratchmnt /var/lib/containers/storage/overlay/be2eaecf9f74b6acfe4d0017dd5534fde06b2fa8de9ed875691f6ccc791c1836/merged在全新镜像中初始化 RPM 数据库,并添加
redhat-release软件包:# yum install -y --releasever=8 --installroot=$scratchmnt redhat-release将
httpd服务安装到scratch目录中:# yum install -y --setopt=reposdir=/etc/yum.repos.d \ --installroot=$scratchmnt \ --setopt=cachedir=/var/cache/dnf httpd创建
$scratchmnt/var/www/html/index.html文件:# mkdir -p $scratchmnt/var/www/html # echo "Your httpd container from scratch works!" > $scratchmnt/var/www/html/index.html配置
working-container以直接从容器运行httpd守护进程:# buildah config --cmd "/usr/sbin/httpd -DFOREGROUND" working-container # buildah config --port 80/tcp working-container # buildah commit working-container localhost/myhttpd:latest
验证
列出本地存储中的所有镜像:
# podman images REPOSITORY TAG IMAGE ID CREATED SIZE localhost/myhttpd latest 08da72792f60 2 minutes ago 121 MB运行
localhost/myhttpd镜像,并在容器和主机系统之间配置端口映射:# podman run -p 8080:80 -d --name myhttpd 08da72792f60测试 Web 服务器:
# curl localhost:8080 Your httpd container from scratch works!