Skip to content

Buildah Basics

homepage-banner

Introduction

The OCI specification is an open standard, which encourages the development of multiple open-source implementations for the container engine and container image building mechanism. Two increasingly popular examples today are Podman and Buildah.

Buildah is a command-line tool that allows you to build, modify, and inspect container images without the need for a full Docker daemon. Buildah is designed to be lightweight and fast, while still providing you with all the necessary tools to create, modify, and manage container images. Buildah is written in Go and is available for Linux, macOS, and Windows.

Example 1: Building an Image with Buildah

To build an image with Buildah, you first need to create a container from a base image. For example, to create a container from the CentOS 8 base image, you would run the following command:

buildah from centos:8

This command will create a new container based on the CentOS 8 base image. You can then use the buildah run command to execute commands on the container, such as installing packages or modifying files. For example, to install the Apache web server on the container, you would run the following command:

buildah run mycontainer dnf install -y httpd

Once you have made all the necessary changes to the container, you can then use the buildah commit command to create a new image from the modified container. For example, to create a new image called myimage, you would run the following command:

buildah commit mycontainer myimage

Example 2: Building an Image from a Dockerfile with Buildah

Buildah also allows you to build images from Dockerfiles. To build an image from a Dockerfile, you first need to create a new container with the buildah from command. You can then use the buildah add command to copy the Dockerfile into the container, and the buildah config command to set the working directory to the location of the Dockerfile. For example, to build an image from a Dockerfile located in the current directory, you would run the following commands:

buildah from centos:8
buildah add mycontainer Dockerfile
buildah config --workingdir /myapp mycontainer

You can then use the buildah run command to execute the Dockerfile commands on the container, and the buildah commit command to create a new image from the modified container. For example, to build an image called myimage from the Dockerfile, you would run the following commands:

buildah run mycontainer yum install -y httpd
buildah run mycontainer systemctl enable httpd
buildah commit mycontainer myimage

Example 3: Pushing an Image to a Registry with Buildah

Buildah also allows you to push images to container registries, such as Docker Hub or Quay.io. To push an image to a registry, you first need to tag the image with the registry URL and the image name. For example, to tag the myimage image with the Docker Hub registry, you would run the following command:

buildah tag myimage docker.io/myusername/myimage

Once you have tagged the image, you can then use the buildah push command to push the image to the registry. For example, to push the myimage image to Docker Hub, you would run the following command:

buildah push docker.io/myusername/myimage

Conclusion

Buildah is a lightweight and fast tool for building, modifying, and managing container images. With Buildah, you can build images from scratch, modify existing images, and push images to container registries. Buildah is a great alternative to Docker for those who want a more lightweight and flexible way to manage their container images. Give it a try and see how it can help streamline your container workflow.

References

  • https://github.com/containers/buildah/blob/main/install.md
Leave a message