Buildah is an open-source tool for building and managing container images. It was developed by Red Hat as a lightweight alternative to Docker. Buildah focuses on the fundamental aspects of creating, modifying and storing container images without requiring a container runtime.
Buildah allows to:
- Create new container images from scratch or existing ones.
- Build images using Dockerfiles, with similar functionality to “docker build.”
- Modify images by adding, deleting, or modifying files and metadata.
- Commit the changes to a new image.
- Push and pull images to and from container registries.
Buildah is part of a larger ecosystem of container tools that Red Hat has been developing, which includes Podman, Skopeo, and CRI-O. These tools are meant to provide a more modular and flexible approach to container management, allowing users to choose and use the tools they need for their specific use cases.
Buildah is an excellent tool for creating and managing container images. In this guide, we will walk you through the process of installing Buildah, creating a new container image, modifying it, and pushing it to a container registry. Let’s get started!
Step 1: Installing Buildah
Before start using Buildah, we need to install it into the system. The installation process may vary depending on your operating system. For the most up-to-date instructions, please refer to the official Buildah documentation.
On Ubuntu
sudo apt-get update
sudo apt-get install buildah
On Fedora
sudo dnf install buildah
Step 2: Creating a New Container Image
Now that we have Buildah installed, let’s create a new container image from scratch.
Starting a New Image with Buildah
To start a new image, we use the buildah from command followed by the base image we want to use. For example, to create a new image based on the Alpine Linux image, we run:
buildah from alpine
Listing Container Images with Buildah
To see a list of all the container images on your system, run the buildah images command:
buildah images
Step 3: Modifying a Container Image throw with Buildah
With our new image in place, let’s modify it by adding files, updating metadata, and installing additional software.
Running Commands Inside the Container
To execute commands inside the container, use the buildah run command. For example, to update the package index and install curl, we run:
buildah run CONTAINER_ID -- apk update
buildah run CONTAINER_ID -- apk add curl
Replace CONTAINER_ID with the ID of your container.
Adding Files by Buildah copy
To add files to the container, use the buildah copy command:
buildah copy CONTAINER_ID local-file.txt /path/in/container/
Setting Metadata
To set metadata for the container image, such as entrypoint or environment variables, use the buildah config command:
buildah config --entrypoint '["/usr/bin/my-entrypoint.sh"]' CONTAINER_ID
buildah config --env KEY=VALUE CONTAINER_ID
Step 4: Building Images with Dockerfiles
If you have a Dockerfile, you can use Buildah to build an image from it. To do so, run the buildah bud command followed by the path to your Dockerfile:
buildah bud -f /path/to/Dockerfile -t my-image:latest .
Step 5: Pushing and Pulling Images
With our modified image ready, let’s push it to a container registry.
Tagging the Image
First, we need to tag our image with the registry URL:
buildah tag my-image:latest registry.example.com/my-image:latest
Logging In to the Registry
To authenticate with the container registry, run the buildah login command:
buildah login registry.example.com
Pushing the Image with Buildah
Finally, to push our image to the registry, use the buildah push command:
buildah push registry.example.com/my-image:latest
Pulling Images with Buildah
To pull an image from a container registry, you can use the buildah pull command followed by the image name:
buildah pull registry.example.com/my-image:latest
This will download the image to your local system, making it available for use with Buildah or other container tools.
Conclusion
In this step-by-step guide, we covered the basics of using Buildah to create, modify, and manage container images. Buildah is a powerful and flexible tool that allows you to work with container images without requiring a full container runtime like Docker. By following this guide, you should now have a good understanding of how to use Buildah to build, customize, and share your own container images.
You May Also Like: How to install CRI-OFollow us on LinkedIn for updates!