Skip to main content
Redhat Developers  Logo
  • Products

    Featured

    • Red Hat Enterprise Linux
      Red Hat Enterprise Linux Icon
    • Red Hat OpenShift AI
      Red Hat OpenShift AI
    • Red Hat Enterprise Linux AI
      Linux icon inside of a brain
    • Image mode for Red Hat Enterprise Linux
      RHEL image mode
    • Red Hat OpenShift
      Openshift icon
    • Red Hat Ansible Automation Platform
      Ansible icon
    • Red Hat Developer Hub
      Developer Hub
    • View All Red Hat Products
    • Linux

      • Red Hat Enterprise Linux
      • Image mode for Red Hat Enterprise Linux
      • Red Hat Universal Base Images (UBI)
    • Java runtimes & frameworks

      • JBoss Enterprise Application Platform
      • Red Hat build of OpenJDK
    • Kubernetes

      • Red Hat OpenShift
      • Microsoft Azure Red Hat OpenShift
      • Red Hat OpenShift Virtualization
      • Red Hat OpenShift Lightspeed
    • Integration & App Connectivity

      • Red Hat Build of Apache Camel
      • Red Hat Service Interconnect
      • Red Hat Connectivity Link
    • AI/ML

      • Red Hat OpenShift AI
      • Red Hat Enterprise Linux AI
    • Automation

      • Red Hat Ansible Automation Platform
      • Red Hat Ansible Lightspeed
    • Developer tools

      • Red Hat Trusted Software Supply Chain
      • Podman Desktop
      • Red Hat OpenShift Dev Spaces
    • Developer Sandbox

      Developer Sandbox
      Try Red Hat products and technologies without setup or configuration fees for 30 days with this shared Openshift and Kubernetes cluster.
    • Try at no cost
  • Technologies

    Featured

    • AI/ML
      AI/ML Icon
    • Linux
      Linux Icon
    • Kubernetes
      Cloud icon
    • Automation
      Automation Icon showing arrows moving in a circle around a gear
    • View All Technologies
    • Programming Languages & Frameworks

      • Java
      • Python
      • JavaScript
    • System Design & Architecture

      • Red Hat architecture and design patterns
      • Microservices
      • Event-Driven Architecture
      • Databases
    • Developer Productivity

      • Developer productivity
      • Developer Tools
      • GitOps
    • Secure Development & Architectures

      • Security
      • Secure coding
    • Platform Engineering

      • DevOps
      • DevSecOps
      • Ansible automation for applications and services
    • Automated Data Processing

      • AI/ML
      • Data Science
      • Apache Kafka on Kubernetes
      • View All Technologies
    • Start exploring in the Developer Sandbox for free

      sandbox graphic
      Try Red Hat's products and technologies without setup or configuration.
    • Try at no cost
  • Learn

    Featured

    • Kubernetes & Cloud Native
      Openshift icon
    • Linux
      Rhel icon
    • Automation
      Ansible cloud icon
    • Java
      Java icon
    • AI/ML
      AI/ML Icon
    • View All Learning Resources

    E-Books

    • GitOps Cookbook
    • Podman in Action
    • Kubernetes Operators
    • The Path to GitOps
    • View All E-books

    Cheat Sheets

    • Linux Commands
    • Bash Commands
    • Git
    • systemd Commands
    • View All Cheat Sheets

    Documentation

    • API Catalog
    • Product Documentation
    • Legacy Documentation
    • Red Hat Learning

      Learning image
      Boost your technical skills to expert-level with the help of interactive lessons offered by various Red Hat Learning programs.
    • Explore Red Hat Learning
  • Developer Sandbox

    Developer Sandbox

    • Access Red Hat’s products and technologies without setup or configuration, and start developing quicker than ever before with our new, no-cost sandbox environments.
    • Explore Developer Sandbox

    Featured Developer Sandbox activities

    • Get started with your Developer Sandbox
    • OpenShift virtualization and application modernization using the Developer Sandbox
    • Explore all Developer Sandbox activities

    Ready to start developing apps?

    • Try at no cost
  • Blog
  • Events
  • Videos

Build and run Buildah inside a Podman container

April 4, 2019
Tom Sweeney
Related topics:
Containers

Share:

    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

    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.

    Last updated: October 17, 2019

    Recent Posts

    • Assessing AI for OpenShift operations: Advanced configurations

    • OpenShift Lightspeed: Assessing AI for OpenShift operations

    • OpenShift Data Foundation and HashiCorp Vault securing data

    • Axolotl meets LLM Compressor: Fast, sparse, open

    • What’s new for developers in Red Hat OpenShift 4.19

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Products

    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform

    Build

    • Developer Sandbox
    • Developer Tools
    • Interactive Tutorials
    • API Catalog

    Quicklinks

    • Learning Resources
    • E-books
    • Cheat Sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site Status Dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Report a website issue