3

使用buildah,我正在尝试构建一个容器镜像,该镜像只应包含程序cowsay及其依赖项。我在没有 root 权限的Fedora 29计算机上执行此操作。而不是使用 Dockerfile 和命令buildah build-using-dockerfile(又名buildah bud)我想

  1. 创建一个空容器。
  2. 挂载它的文件系统。
  3. 在我的主机系统上使用/usr/bin/dnf将软件包直接安装到挂载点下的文件系统中。
  4. 从文件系统创建容器映像。

该命令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 用户,如何从头开始构建容器映像?

4

1 回答 1

2

短篇故事

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 configbuildah config --created-bybuildah 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 ~]$ 
于 2019-02-18T18:42:20.003 回答