我尝试使用buildah建立一个容器
仅应包含程序cowsay及其依赖项的映像。
我是在没有root权限的Fedora 29计算机上执行此操作的。我不想使用Dockerfile和命令buildah build-using-dockerfile
(也称为buildah bud
)
/usr/bin/dnf
将软件包直接安装到安装点下的文件系统中。 命令buildah from scratch
创建一个空容器,但是当我尝试挂载文件系统时出现错误
[testuser@linux ~]$ container=$(buildah from scratch)
[testuser@linux ~]$ mnt=$(buildah mount $container)
cannot mount using driver overlay in rootless mode
ERRO[0000] exit status 1
[testuser@linux ~]$
更多信息
[testuser@linux ~]$ cat /etc/redhat-release
Fedora release 29 (Twenty Nine)
[testuser@linux ~]$ buildah --version
buildah version 1.6 (image-spec 1.0.0, runtime-spec 1.0.0)
[testuser@linux ~]$
出了什么问题?如何以非root用户身份从头开始构建容器映像?
答案 0 :(得分:1)
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 ~]$
可以通过添加一些buildah config
命令来提供一些元数据信息(例如buildah config --created-by
和buildah config --cmd
)来改进 build.sh 脚本。
除了使用脚本 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 ~]$