如何使用 Buildah 构建容器镜像
Buildah 容器
![](/data/attachment/album/201805/21/071923ewuygn5g3vggjf3n.png)


Project Atomic 通过他们在 Open Container Initiative（OCI）上的努力创造了一个名为 [Buildah](https://github.com/projectatomic/buildah) 的伟大工具。Buildah 能帮助创建、构建和更新，它支持 Docker 容器镜像以及 OCI 兼容镜像。


Buildah 处理构建容器镜像时无需安装完整的容器运行时或守护进程。这对建立容器的持续集成和持续交付管道尤其有用。


Buildah 使容器的文件系统可以直接供构建主机使用。这意味着构建工具在主机上可用就行，而不需要在容器镜像中可用，从而使构建更快速，镜像更小，更安全。Buildah 有 CentOS、Fedora 和 Debian 的软件包。


### 安装 Buildah


从 Fedora 26 开始 Buildah 可以使用 `dnf` 进行安装。



```
$ sudo dnf install buildah -y

```

`buildah` 的当前版本为 0.16，可以通过以下命令显示。



```
$ buildah --version

```

### 基本命令


构建容器镜像的第一步是获取基础镜像，这是通过 Dockerfile 中的 `FROM` 语句完成的。Buildah 以类似的方式处理这个。



```
$ sudo buildah from fedora

```

该命令将拉取 Fedora 的基础镜像并存储在主机上。通过执行以下操作可以检查主机上可用的镜像。



```
$ sudo buildah images
IMAGE ID IMAGE NAME CREATED AT SIZE
9110ae7f579f docker.io/library/fedora:latest Mar 7, 2018 20:51 234.7 MB

```

在拉取基础镜像后，有一个该镜像的运行容器实例，这是一个“工作容器”。


以下命令显示正在运行的容器。



```
$ sudo buildah containers
CONTAINER ID BUILDER IMAGE ID IMAGE NAME
CONTAINER NAME
6112db586ab9 * 9110ae7f579f docker.io/library/fedora:latest fedora-working-container

```

Buildah 还提供了一个非常有用的命令来停止和删除当前正在运行的所有容器。



```
$ sudo buildah rm --all

```

完整的命令列表可以使用 `--help` 选项。



```
$ buildah --help

```

### 构建一个 Apache Web 服务器容器镜像


让我们看看如何使用 Buildah 在 Fedora 基础镜像上安装 Apache Web 服务器，然后复制一个可供服务的自定义 `index.html`。


首先让我们创建自定义的 `index.html`。



```
$ echo "Hello Fedora Magazine !!!" > index.html

```

然后在正在运行的容器中安装 httpd 包。



```
$ sudo buildah from fedora
$ sudo buildah run fedora-working-container dnf install httpd -y

```

让我们将 `index.html` 复制到 `/var/www/html/`。



```
$ sudo buildah copy fedora-working-container index.html /var/www/html/index.html

```

然后配置容器入口点以启动 httpd。



```
$ sudo buildah config --entrypoint "/usr/sbin/httpd -DFOREGROUND" fedora-working-container

```

现在为了使“工作容器”可用，`commit` 命令将容器保存到镜像。



```
$ sudo buildah commit fedora-working-container hello-fedora-magazine

```

hello-fedora-magazine 镜像现在可用，并且可以推送到仓库以供使用。



```
$ sudo buildah images
IMAGE ID IMAGE NAME CREATED
AT SIZE
9110ae7f579f docker.io/library/fedora:latest
Mar 7, 2018 22:51 234.7 MB
49bd5ec5be71 docker.io/library/hello-fedora-magazine:latest
Apr 27, 2018 11:01 427.7 MB

```

通过运行以下步骤，还可以使用 Buildah 来测试此镜像。



```
$ sudo buildah from --name=hello-magazine docker.io/library/hello-fedora-magazine
$ sudo buildah run hello-magazine

```

访问 <http://localhost> 将显示 “Hello Fedora Magazine !!!”




---


via: <https://fedoramagazine.org/daemon-less-container-management-buildah/>


作者：[Ashutosh Sudhakar Bhakare](https://fedoramagazine.org/author/ashutoshbhakare/) 选题：[lujun9972](https://github.com/lujun9972) 译者：[geekpi](https://github.com/geekpi) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
Project Atomic, through their efforts on the Open Container Initiative (OCI), have created a great tool called [Buildah](https://github.com/projectatomic/buildah). Buildah helps with creating, building and updating container images supporting Docker formatted images as well as OCI compliant images.


Buildah handles building container images without the need to have a full container runtime or daemon installed. This particularly shines for setting up a continuous integration and continuous delivery pipeline for building containers.

Buildah makes the container’s filesystem directly available to the build host. Meaning that the build tooling is available on the host and not needed in the container image, keeping the build faster and the image smaller and safer. There are Buildah packages for CentOS, Fedora, and Debian.

**Installing Buildah**

Since Fedora 26 Buildah can be installed using dnf.

$ sudo dnf install buildah -y

The current version of buildah is 0.16, which can be displayed by the following command.

$ buildah --version

## Basic commands

The first step needed to build a container image is to get a base image, this is done by the FROM statement in a Dockerfile. Buildah does handle this in a similar way.

$ sudo buildah from fedora

This command pulls the Fedora based image and stores it on the host. It is possible to inspect the images available on the host, by running the following.

$ sudo buildah images IMAGE ID IMAGE NAME CREATED AT SIZE 9110ae7f579f docker.io/library/fedora:latest Mar 7, 2018 20:51 234.7 MB

After pulling the base image, a running container instance of this image is available, this is a “working-container”.

The following command displays the running containers.

$ sudo buildah containers CONTAINER ID BUILDER IMAGE ID IMAGE NAME CONTAINER NAME 6112db586ab9 * 9110ae7f579f docker.io/library/fedora:latest fedora-working-container

Buildah also provides a very useful command to stop and remove all the containers that are currently running.

$ sudo buildah rm --all

The full list of command is available using the *–help* option.

$ buildah --help

**Building an Apache web server container image**


Let’s see how to use Buildah to install an Apache web server on a Fedora base image, then copy a custom index.html to be served by the server.

First let’s create the custom index.html.

$ echo "Hello Fedora Magazine !!!" > index.html

Then install the httpd package inside the running container.

$ sudo buildah from fedora $ sudo buildah run fedora-working-container dnf install httpd -y

Let’s copy index.html to /var/www/html/.

$ sudo buildah copy fedora-working-container index.html /var/www/html/index.html

Then configure the container entrypoint to start httpd.

$ sudo buildah config --entrypoint "/usr/sbin/httpd -DFOREGROUND" fedora-working-container

Now to make the “working-container” available, the *commit* command saves the container to an image.

$ sudo buildah commit fedora-working-container hello-fedora-magazine

The *hello-fedora-magazine *image is now available, and can be pushed to a registry to be used.

$ sudo buildah images IMAGE ID IMAGE NAME CREATED AT SIZE 9110ae7f579f docker.io/library/fedora:latest Mar 7, 2018 22:51 234.7 MB 49bd5ec5be71 docker.io/library/hello-fedora-magazine:latest Apr 27, 2018 11:01 427.7 MB

It is also possible to use Buildah to test this image by running the following steps.

$ sudo buildah from --name=hello-magazine docker.io/library/hello-fedora-magazine $ sudo buildah run hello-magazine

Accessing [http://localhost](http://localhost) will display “*Hello Fedora Magazine !!!*“

## pixdrift

You may have issues with Buildah executing entrypoint as this is not the expected behaviour from Buildah. Your final command to ‘buildah run hello-magazine’ should use ‘podman run’ if you want it to use the configured container entrypoint.

## Ashutosh

Had tested and works fine

## pixdrift

The version used for this test may have worked correctly, but future versions may experience issues as it isn’t expected behaviour:

https://github.com/projectatomic/buildah/issues/607

## pixdrift

Apologies, this is related to parameters on entrypoint.

## Hushen Beg

It is very nice Ashutosh sir. I am also working on Aws,Docker,Swarm ,Ec2 etc .Now a days buildah is very useful for updating and building the container images.

## prafful chavan

nice sir….????

Is that same way to install in redhat as well or not ?

## Erik

Being in New England, gotta love the name! (It is how Builder would be pronounced with a local accent).

## Paul W. Frields

@Erik: That’s exactly how it got its name — a tribute to the very pronounced accent of Dan Walsh, who works on container technologies.

## FeRDNYC

I’m a little disappointed, though, because at no point does anyone ever point out that Buildah is “a wicked good tool for maintaining yah containahs”. #MissedOpportunity

## Madhusudan Upadhyay

“Buildah makes the container’s filesystem directly available to the build host”

That is good news. Perhaps, utilities from container host can be leveraged as a part of the build now and that would in turn enhance the overall performance of the build process.

Impressive and quite self-explanatory article Ashutosh Sir. Good work.

## umesh

It is very nice Ashutosh sir. I am also working on docker .Now a days buildah is very useful for updating and building the container images.

It is easy to use.

## Akshay Wani

Very helpful article to start with Buildah . People currently working on Docker should definately try out Buildah.

## Keecheril Jobin Varghese

Perfectly explained. Thank you for making it easier to understand!

## clime

Personally, I don’t understand the ‘sudos’ in every single command. Do they really need to be there? I would like to see a tool that is able to create a container image from host without having to use a privileged user.

## Athavan Kanapuli

Does buildah allows more than one process to run inside a single container? Taking back the httpd example in the article, I need redis server to run in the same container along with httpd. Does builah has an init system to manage multiple services?