Build and run Buildah inside a Podman container

Build and run Buildah inside a Podman container

This past Christmas I gave my wife a set of nesting dolls similar to Russian Matryoshka dolls. If you’re not familiar with them, they consist of a wooden doll, which opens to reveal another doll, inside which you’ll find another doll, and so on until you get to the smallest and often most ornate doll of them all.  This concept got me thinking about nesting containers.

I thought I’d try building my own nesting container using Podman to create a container in which I could do Buildah development and also spin up Buildah containers and images. Once this Podman container was created, I could move it to any Linux platform that supported Podman and do development on Buildah from it. In this article, I’ll show how I set it up.

Preparing the environment

I started this experiment on a newly installed Fedora 29 virtual machine and installed the latest Podman and container-selinux on it with dnf -y install podman container-selinux --enablerepo updates-testing. This gave me Podman v1.1.2 and container-selinux version 2.85-1.

Because both the container and the container within the container will be using fuse-overlayfs, they won’t be happy trying to mount their respective directories over each other. So, the first step is to create a directory for the container within the container to use, and I’ve named it /var/lib/mycontainer:

# mkdir /var/lib/mycontainer

Everything you need to grow your career.

With your free Red Hat Developer program membership, unlock our library of cheat sheets and ebooks on next-generation application development.

SIGN UP

Podman container creation

I then created the following Dockerfile, which will pull Fedora, set up the GOPATH, install the Buildah dependencies, use git to clone the project in the /root/buildah directory, and finally update /etc/container/storage.conf with sed to uncomment the mount_program:

# FILE=~/Dockerfile.cinc
# /bin/cat <<EOM >$FILE
FROM fedora:latest
ENV GOPATH=/root/buildah

RUN dnf -y install \
make \
golang \
bats \
btrfs-progs-devel \
device-mapper-devel \
glib2-devel \
gpgme-devel \
libassuan-devel \
libseccomp-devel \
ostree-devel \
git \
bzip2 \
go-md2man \
runc \
fuse-overlayfs \
fuse3 \
containers-common; \
mkdir /root/buildah; \
git clone https://github.com/containers/buildah /root/buildah/src/github.com/containers/buildah

RUN sed -i -e 's|#mount_program = "/usr/bin/fuse-overlayfs"|mount_program = "/usr/bin/fuse-overlayfs"|' /etc/containers/storage.conf
EOM

Next we create the image using the Dockerfile. (Note the hard-to-see period at the end of the line.) This command can take 5 to 10 minutes to complete, and it seems to hang for a bit near the end, so be patient. This is one of those great commands that you can kick off and let run while you go freshen up your cup of tea.

# podman build -t buildahimage -f ~/Dockerfile.cinc .

That’s it for the heavy lifting. Let’s create a Podman container in which we’ll do our Buildah development. The following command creates a container named buildahctr, mounts the host’s mycontainer to the container’s containers directory, runs the container detached using the host’s network, turns off label and seccomp confinement in the container, and finally does a little shell hackery to keep the container up and running.

# podman run --detach --name=buildahctr --net=host --security-opt label=disable --security-opt seccomp=unconfined --device /dev/fuse:rw -v /var/lib/mycontainer:/var/lib/containers:Z   buildahimage sh -c 'while true ;do wait; done'

Buildah development

Great, now we have a container up and running Fedora. Let’s hop in and compile and install Buildah onto it. The following command will get us to the command line inside the container.

# podman exec -it buildahctr /bin/sh

Now that we’re inside the container, it’s time for some standard make, git, and Buildah running. (Note that the next five commands were run at the sh-4.4# prompt; I’ve removed several of the prompts for easier cutting and pasting.)

sh-4.4# cd /root/buildah
export GOPATH=`pwd`
cd /root/buildah/src/github.com/containers/buildah
make
make install

sh-4.4# buildah from alpine
alpine-working-container

sh-4.4# buildah images
REPOSITORY               TAG    IMAGE  ID    CREATED    SIZE
docker.io/library/alpine latest 5cb3aa00f899 9 days ago 5.79 MB

So now we’ve compiled, installed, and run Buildah from within a Podman container.

Next, let’s do a quick change to the Buildah source and see if we can run that with the change in place. Use vi or your favorite editor to change cmd/buildah/images.go. Search for the outputHeader() function (near line 219) and find the line in it: format := "table {{.Name}}\t{{.Tag}}\t". Remove the word “table” from that line making it format := "{{.Name}}\t{{.Tag}}\t”. Save the file, exit, then make and make install it again.

sh-4.4# vi cmd/buildah/images.go
sh-4.4# make
sh-4.4# make install

Now if you run buildah images, you should see that we’ve made all output act as if the --quiet option is on, which doesn’t show the table headers.

sh-4.4# buildah images
docker.io/library/alpine  latest 5cb3aa00f899 9 days ago   5.79 MB

Running a buildah container inside of a Podman container

For one last piece of fun, let’s see if we can run a Buildah container within this Podman container using our modified Buildah code. We’ll just do something simple and list the contents of the / directory.

sh-4.4# buildah from --name myalpine alpine
myalpine

sh-4.4# buildah run --isolation=chroot myalpine ls /
bin   dev   etc  home   lib   media   mnt  opt   proc   root run   sbin   srv   sys   tmp   usr   var

A portable Buildah development environment

If you’ve made it this far, you’ve built a container using Podman that is capable of doing Buildah development. That container can also build containers and then run those containers.  I used Buildah for this example, but I could have used Podman to build the internal container, too. Regardless of the internal tool chosen, I now have a container that can build and run containers within itself, much like Matryoshka dolls. Best yet, I could commit this container and push it out to Quay.io or another container registry and then pull it down and run it on another Fedora machine or even another Linux platform. I’ve made a totally portable Buildah development environment.

I hope this exercise will get you thinking about how you might use Podman and Buildah to create a more flexible and dynamic environment in your own shop. Another blog is underway that will show how to do this without being the root user, so stay tuned!

P.S. No daemons were harmed nor deployed during this demo.

To learn more, visit our Linux containers or microservices Topic pages.

To learn more, visit our Join the Red Hat Developer Program (it’s free) and get access to related cheat sheets (e.g. containers), books (e.g. microservices), and product downloads that can help you with your microservices and/or container application development.

Share