Skip to main content

Getting Started With Buildah for Managing Linux Containers

Buildah is used to create, build, manage, run container images as well as containers. It can be used with Docker, Podman, Kubernetes! Let's get started with Buildah.

LHB Community

Warp Terminal

Docker,  Docker, Docker” that is what we used until recently.

Docker is still very popular, no doubt. But as we know in the IT industry, evolution happens rapidly. So now we will hear Buildah, Podman, CRI-O, Skopeo.

If you want to relate with an analogy – we have various types of berries such as black berries, blueberries, raspberries but someday someone decides to choose blueberries. So among Buildah, Podman, CRI-O I decided to choose buildah. Why? Because it’s as interesting as other container platforms.

Let’s dive into Buildah!

What is Buildah?

Buildah Logo in PNG format

Buildah is a command line tool to build Open Container Initiative (OCI) images. It can be used with Docker, Podman, Kubernetes – whichever is your favourite container tool!

Buildah is used to create, build, manage, run container images as well as containers.

With Buildah you create a working container either from an image or from scratch, You can also create an image from a working container or using Dockerfile. You can build the images in OCI format or the upstream Docker format. You can modify and delete containers and images.

The most interesting thing about Buildah is, it doesn’t need a daemon for it to work. So say no to #bigfatdaemons. Having no daemon specifically gives sparks when it comes to continuous integration and continuous delivery in order to build containers.

No daemon also means you can eliminate Docker’s daemon by Buildah to build container images. But that doesn’t mean Buildah can’t incorporate with Docker. Because using Dockerfile you can build, manage and run your container image by Buildah. Impressive right?

Installing Buildah on Linux

Let’s see how to get your hands dirty with Buildah. Packages for Buildah is available for Fedora and CentOS. You can easily install them using the package manager of your Linux distribution. I am going to list some of the common ones here:

To install Buildah on Fedora, use

dnf install buildah -y

To install Buildah on CentOS or RedHat:

yum install buildah -y

To install Buildah on Ubuntu and Debian, you can use the official PPA provided by Project Atomic.

sudo apt update
sudo apt install -y software-properties-common
sudo add-apt-repository -y ppa:projectatomic/ppa
sudo apt install buildah

Once Buildah is installed, it’s time to play with it.

Basic commands to use Buildah

Geeting started with Buildah with a sample project

Here are some basic commands to start with buildah.

To know version of Buildah.

buildah --version

The very initial task is to pull the container image from public or private repositories. To pull image buildah uses ‘from’ variable. ‘from’ variable is same as it is used in Dockerfile. The difference is in Docker we specify the variable ‘from’ in Dockerfile, where in Buildah we are passing the variable via command line.

buildah from ubuntu

In above command Ubuntu is my container image. After pulling Ubuntu based image successfully at the end of the ouput you will see – “ubuntu-working-container”.

After you know you have pulled image successfully, now you may want to see list of images you pulled. But before that, you may be wondering “where are these images get stored on?”. The pulled image gets stored on the host itself.

buildah images 
 IMAGE NAME                                               IMAGE TAG            IMAGE ID             CREATED AT             SIZE                                 latest               94e814e2efa8         Mar 12, 2019 00:20     91.4 MB                                 latest               d09302f77cfc         Mar 12, 2019 00:20     283 MB

If you already are familiar with Docker commands, then you might have noticed the similarities in Docker and Buildah commands. For example, in Docker to see the list of pulled images we use ‘docker images’ command.

Here is the command to see the list of running containers. In Buildah as soon as the container image pulling is done completely, that container starts to run. If comparing to Docker, after pulling the image, we need to run the image by using ‘docker run’ and then only container starts running.

 1063320d2dc0     *     94e814e2efa8  ubuntu-working-container
 978a399e0aa7     *     d09302f77cfc  fedora-working-container

If you want to clean up and remove all runing containers then execute below command with caution. Because once you execute the command, your containers will be removed. The privilege we get in buildah over Docker is, Docker does not let us remove the running container but Buildah does. While using Docker first we have to stop the container and then only we can remove the image.

buildah rm --all

If you are stuck with some command, there is the –help option.

buildah --help

Let’s see some use cases of Buildah, to start with hands-on implementation we will start with simple. And once you find the pace with the Buildah, you can pour up your creativity and can do interesting stuffs.

Building an Apache web server container image [Hands On]

Let’s try what you just learned with a sample Buildah project.

What do you think what will be needed to have an Apache web server running inside a container?

Of course, an image, a running container and what else? Well, the main thing we will need is – we need to install a httpd package inside a container. Oh! how do we do that? No worries. I will tell you that.

Let’s first pull a Linux distribution image. Choose whichever is yours favorite. I have pulled CentOS base image.

buildah from centos

Now install httpd inside the CentOS container. You can do that using this command:

buildah run centos-working-container yum install httpd -y

You’ll see an output like this.

What we did is, we installed the httpd package without even going inside in the container. It’s like setting up your bike’s parts without even entering in garage. You can see in above command’s output, the package has been installed in front of us. Buildah does no cheating 😉

Now, let’s create custom index.html file.

echo "Linux Handbook is interesting !" > index.html

Copy your custom file index.html to the directory path /var/www/html

buildah copy centos-working-container index.html /var/www/html/index.html

You must be wondering where did /var/www/html come from? And why did we copy index.html file under that directory path?

When we install httpd package, /var/www/html directory gets created. /var/www/html is just the default root folder of the web server.

In order to start container, we need to comfigure entrypoint for the container.

buildah config --entrypoint "/usr/sbin/httpd -DFOREGROUND" centos-working-container

“-DFOREGROUND” option is used along with “/usr/sbin/httpd ” to start httpd. What “-DFOREGROUND” does is, it makes httpd/apache process run in the foreground instead if background. The benefit of httpd process NOT running in the background is that you can see the debug logs on the console. If we do not use “-DFOREGROUND” option, the apache server will start and right after that, it will be stopped.

Now, we need to save whatever we have done inside the container. The commit will do it for us.

buildah commit centos-working-container  Linux-Handbook

The Linux-Handbook image is available and you can push this image to registry to use it.

buildah images
Mar 31, 2019 14:36 234.7 MB
Apr 4, 2019 17:28 427.7 MB

So, all set up to get your hands dirty with Buildah?

Written by Servesha Dudhgaonkar

LHB Community