containersbuildah

How to build a container image from scratch as a non-root user?


With buildah I am trying to build a container image that should only contain the program cowsay and its dependencies. I am doing this on a Fedora 29 computer where I don't have root permissions. Instead of using a Dockerfile and the command buildah build-using-dockerfile (aka buildah bud) I would like to

  1. Create an empty container.
  2. Mount its file system.
  3. Use /usr/bin/dnf on my host system to install packages directly into the file system under the mount point.
  4. Create the container image from the file system.

The command buildah from scratch creates an empty container but when I try to mount the file system I get an error

[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 ~]$ 

Some more information

[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 ~]$ 

What goes wrong? How can I build the container image from scratch as a non-root user?


Solution

  • Short story

    buildah unshare is needed to create an unshare environment. That was missing which led to the error message cannot mount using driver overlay in rootless mode .

    To build the container image create the file build.sh with this content

    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
    

    Then run the script build.sh inside an unshare environment

    [testuser@linux ~]$ buildah unshare bash build.sh
    

    List all images to see the newly built container image

    [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 ~]$ 
    

    To try out the the newly built container image run

    [testuser@linux ~]$ podman run localhost/cowsay-container1 cowsay hello                                                                                                                                
     _______                                                                                                                                                                                                
    < hello >                                                                                                                                                                                               
     -------                                                                                                                                                                                                
            \   ^__^                                                                                                                                                                                        
             \  (oo)\_______                                                                                                                                                                                
                (__)\       )\/\                                                                                                                                                                            
                    ||----w |                                                                                                                                                                               
                    ||     ||                                                                                                                                                                               
    [testuser@linux ~]$
    

    The build.sh script could be improved by adding some buildah config commands to provide some metadata information (e.g. buildah config --created-by and buildah config --cmd).

    Longer story

    Instead of building the container image with the script build.sh, it is also possible to step into the unshare environment and run build commands manually.

    [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 ~]$