深入了解Buildah:从零开始构建容器
立即解锁
发布时间: 2025-08-14 00:33:12 阅读量: 10 订阅数: 14 


Podman for DevOps: 使用容器化技术革新
### 深入了解 Buildah:从零开始构建容器
#### 1. 准备环境
Buildah 可在不同的发行版上使用,并且可以通过各自的包管理器进行安装。以下是在主要发行版上安装 Buildah 的示例:
| 发行版 | 安装命令 |
| ---- | ---- |
| Fedora | `sudo dnf -y install buildah` |
| Debian (Bullseye 或更高版本) | `sudo apt-get update` <br> `sudo apt-get -y install buildah` |
| CentOS | `sudo yum install -y buildah` |
| RHEL8 | `sudo yum module enable -y container-tools:1.0` <br> `sudo yum module install -y buildah` |
| RHEL7 | `sudo subscription-manager repos --enable=rhel-7-server-extras-rpms` <br> `sudo yum -y install buildah` |
| Arch Linux | `sudo pacman –S buildah` |
| Ubuntu (20.10 或更高版本) | `sudo apt-get -y update` <br> `sudo apt-get -y install buildah` |
| Gentoo | `sudo emerge app-emulation/libpod` |
此外,Buildah 也可以从源代码构建,相关指南可参考:https://github.com/containers/buildah/blob/main/install.md#building-from-scratch 。同时,Buildah 还能作为容器进行部署,并以嵌套方式在其中执行构建。
#### 2. 验证安装
安装 Buildah 后,可运行一些基本测试命令来验证安装是否成功。
- 查看主机本地存储中的所有可用镜像:
```bash
buildah images
```
此命令输出的镜像列表与 `podman images` 命令的输出相同,因为它们共享相同的本地存储。这两个命令既可以以非特权用户身份执行,也可以以 root 用户身份执行,分别指向用户无 root 权限的本地存储和系统范围的本地存储。
- 运行简单的测试构建来验证安装:
以下是一个简单的测试脚本,用于创建一个最小的 Python 3 镜像:
```bash
#!/bin/bash
BASE_IMAGE=alpine
TARGET_IMAGE=python3-minimal
if [ $UID != 0 ]; then
echo "### Running build test as unprivileged user"
else
echo "### Running build test as root"
fi
echo "### Testing container creation"
container=$(buildah from $BASE_IMAGE)
if [ $? -ne 0 ]; then
echo "Error initializing working container"
fi
echo "### Testing run command"
buildah run $container apk add --update python3 py3-pip
if [ $? -ne 0 ]; then
echo "Error on run build action"
fi
echo "### Testing image commit"
buildah commit $container $TARGET_IMAGE
if [ $? -ne 0 ]; then
echo "Error committing final image"
fi
echo "### Removing working container"
buildah rm $conta
```
0
0
复制全文
相关推荐










