短篇故事
buildah unshare
需要创建一个不共享的环境。缺少这导致错误消息
无法在无根模式下使用驱动程序覆盖安装。
要构建容器映像,请使用此内容创建文件build.sh
container=$(buildah from scratch)
mnt=$(buildah mount $container)
LC_ALL=C dnf install --installroot $mnt --release 29 --setopt=install_weak_deps=False -q -y cowsay
LC_ALL=C dnf --installroot $mnt clean all
buildah umount $container
buildah commit $container cowsay-container1
然后在unshare环境中运行脚本build.sh
[testuser@linux ~]$ buildah unshare bash build.sh
列出所有镜像查看新建的容器镜像
[testuser@linux ~]$ buildah images
IMAGE NAME IMAGE TAG IMAGE ID CREATED AT SIZE
localhost/cowsay-container1 latest 9d9b88a8d5f1 Feb 18, 2019 17:26 307 MB
[testuser@linux ~]$
试用新建的容器镜像运行
[testuser@linux ~]$ podman run localhost/cowsay-container1 cowsay hello
_______
< hello >
-------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
[testuser@linux ~]$
可以通过添加一些命令来提供一些元数据信息(例如和)来改进build.sh脚本。buildah config
buildah config --created-by
buildah config --cmd
更长的故事
除了使用脚本build.sh构建容器镜像,还可以进入unshare环境并手动运行构建命令。
[testuser@linux ~]$ cat /etc/redhat-release
Fedora release 29 (Twenty Nine)
[testuser@linux ~]$ buildah unshare
[root@linux ~]# container=$(buildah from scratch)
[root@linux ~]# mnt=$(buildah mount $container)
[root@linux ~]# LC_ALL=C dnf install --installroot $mnt --release 29 --setopt=install_weak_deps=False -q -y cowsay
warning: /home/testuser/.local/share/containers/storage/overlay/cc67b957fb78eebe6a861a8b69ef4728d0660a636645813224b6ba94fbc80ce0/merged/var/cache/dnf/updates-0b4cc238d1aa4ffe/packages/bash-4.4.23-6.fc29.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 429476b4: NOKEY
Importing GPG key 0x429476B4:
Userid : "Fedora 29 (29) <fedora-29@fedoraproject.org>"
Fingerprint: 5A03 B4DD 8254 ECA0 2FDA 1637 A20A A56B 4294 76B4
From : /etc/pki/rpm-gpg/RPM-GPG-KEY-fedora-29-x86_64
[root@linux ~]# LC_ALL=C dnf --installroot $mnt clean all
33 files removed
[root@linux ~]# buildah umount $container
020ee8e3fb29274a306c441770d2458c732e84076cc0487ce6ea06ac957640d4
[root@linux ~]# buildah commit $container cowsay-container2
Getting image source signatures
Copying blob b3fbecd80150: 292.45 MiB / 292.45 MiB [========================] 2s
Copying config 8aa2ad2933ce: 263 B / 263 B [================================] 0s
Writing manifest to image destination
Storing signatures
8aa2ad2933ce33c8ed8b7551c4a3261177ebd811c9b813b40d5ea77536ac6ef5
[root@linux ~]# exit
exit
[testuser@linux ~]$ buildah images
IMAGE NAME IMAGE TAG IMAGE ID CREATED AT SIZE
localhost/cowsay-container1 latest 9d9b88a8d5f1 Feb 18, 2019 17:26 307 MB
localhost/cowsay-container2 latest 8aa2ad2933ce Feb 18, 2019 17:47 307 MB
[testuser@linux ~]$ podman run localhost/cowsay-container2 cowsay hello
_______
< hello >
-------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
[testuser@linux ~]$