Buildah is a command-line tool for building and managing container images. Unlike Docker, It does not require a daemon to run. It interacts directly with the container runtime, making it suitable for various workflows. Buildah can create OCI (Open Container Initiative) and Docker container images, which ensures compatibility across different platforms. Among the various tools available for managing containers, Buildah stands out for its simplicity and flexibility.
This article will guide you through getting started with Buildah, detailing its features, installation, and practical usage for managing Linux containers.
Installing Buildah
Buildah can be installed on different Linux distributions. Follow the steps below to install Buildah on Ubuntu, Fedora, and CentOS.
Installing Buildah on Fedora
To install Buildah on Fedora, you can use the following command:
# dnf install -y buildah
Installing Buildah on Ubuntu
First, add the necessary repository to install Buildah:
# apt install -y software-properties-common && add-apt-repository -y ppa:projectatomic/ppa
Then install Buildah:
# apt install -y buildah
Installing Buildah on CentOS
On CentOS, you need to enable the Extra Packages for Enterprise Linux (EPEL) repository:
# yum install -y epel-release
Next, install it using the following command:
# yum install -y buildah
Verifying Installation
After installation, verify that Buildah is correctly installed by checking the version:
# buildah --version
Output:
buildah version 1.23.1 (image-spec 1.0.1, runtime-spec 1.0.2-dev)
Creating and Managing Containers
To create a container using Buildah, you can start from a base image and modify it:
# buildah from docker.io/library/alpine
Output:
alpine-working-container
Here, Buildah creates a working container from the Alpine base image and assigns it the name alpine-working-container.
Listing Containers
To list containers managed by Buildah, run:
# buildah containers
Output:
CONTAINER ID BUILDER IMAGE ID IMAGE NAME CONTAINER NAME
f26b3c47e2c3 * 5cb3aa00f899 docker.io/library/alpine:latest alpine-working-container
Mounting a Container
You can mount the container’s filesystem to make changes directly:
# buildah mount alpine-working-container
Output:
/tmp/buildah-mount268375003
Building Container Images
Once you make the desired changes to the container, you can commit them to a new image:
# buildah commit alpine-working-container my-alpine-image
The above command saves the current state of the container alpine-working-container as an image called my-alpine-image.
Building Container Images Using a Dockerfile
To build a container image with a Dockerfile, you first need to create the Dockerfile. Here’s an example of how to create a Dockerfile:
Step 1: Create a Dockerfile
You can create a Dockerfile using your favorite text editor. Let’s create a simple Dockerfile that installs curl on an Alpine Linux-based image.
FROM alpine:latest
RUN apk add --no-cache curl
CMD ["curl", "--version"]
In this Dockerfile:
- FROM alpine: Specifies the base image.
- RUN apk add –no-cache curl: Installs curl in the container.
- CMD [“curl”, “–version”]: Sets the default command to print the installed curl version.
Step 2: Build an Image Using the Dockerfile
Now that we have a Dockerfile, we can use Buildah to build the image:
# buildah bud -t my-docker-image .
This command builds the image using the Dockerfile in the current directory and tags it as my-docker-image. Now, the my-docker-image is ready to use!
Pushing Images to a Registry
To push an image to a container registry, you can use:
# buildah push my-alpine-image docker://docker.io/username/my-alpine-image:latest
Ensure you are logged into the Docker registry to avoid authentication issues:
# buildah login docker.io
Integrating Buildah with Podman
Podman is another daemonless container management tool that integrates seamlessly with Buildah. You can use Podman to run images created by Buildah:
# podman run -it localhost/my-alpine-image /bin/sh
Output:
/ #
You’ll find that using Podman alongside Buildah provides a complete alternative to Docker.
Managing Container Images
To list all available images, use:
# buildah images
Output:
IMAGE NAME IMAGE TAG IMAGE DIGEST IMAGE SIZE
my-alpine-image latest digest 12.4 MB
Inspect an image for more details:
# buildah inspect my-alpine-image
Remove an image from your local storage:
# buildah rmi my-alpine-image
Conclusion
Buildah is a powerful tool for managing Linux containers that offers a lightweight, flexible, and daemonless alternative to traditional container tools. By following this guide, you can quickly start building, modifying, and managing your container images.