聊聊構(gòu)建鏡像開源工具 Buildah
1. 簡介
Buildah[1] 是一種基于 Linux 的開源工具,用于構(gòu)建與開放容器倡議 (OCI) 兼容的容器[2],這意味著容器也與Docker[3]和Kubernetes[4]兼容。借助 Buildah,您可以使用自己喜歡的工具從現(xiàn)有基礎(chǔ)鏡像或使用空鏡像從頭開始創(chuàng)建高效的容器鏡像。這是一種更靈活、更安全的構(gòu)建容器鏡像的方式。
Buildah由 Daniel Walsh 和他在 Red Hat 的團隊于 2017 年創(chuàng)建。他們著手創(chuàng)建容器鏡像的“coreutils”——一種可以與現(xiàn)有容器主機工具一起使用來構(gòu)建 OCI 和 Docker 兼容容器鏡像的工具。然后,這些鏡像可以存儲在容器倉庫[5]中,并在多個運行時環(huán)境[6]中使用。
2. 特點
- 使用或不使用Dockerfiles[7](包含用戶可以調(diào)用以組裝鏡像的所有命令的文本文檔)構(gòu)建容器鏡像
- 從頭開始或從現(xiàn)有容器鏡像起點創(chuàng)建容器鏡像;
- 不在鏡像本身中包含構(gòu)建工具,減少構(gòu)建鏡像的大小,提高安全性,并允許使用更少的資源更容易地傳輸 ;
- 與Dockerfiles 兼容,允許從 Docker 輕松轉(zhuǎn)換;
- 創(chuàng)建特定于用戶的鏡像,以便鏡像可以按創(chuàng)建它們的用戶進行排序;
- 檢查、驗證和修改鏡像;
- 將容器和鏡像從本地存儲推送到公共或私有倉庫或存儲庫;
- 從Docker Hub[8] 推送或拉取鏡像;
- 移除本地存儲的容器鏡像;
- 掛載和卸載工作容器的根文件系統(tǒng);
- 使用容器根文件系統(tǒng)的更新內(nèi)容作為新鏡像的文件系統(tǒng)層。
3. Buildah 和 Podman
Buildah 和Podman[9]都是互補的開源項目和命令行工具,使用并構(gòu)建 OCI 鏡像和容器。首先創(chuàng)建了 Buildah,Podman 使用與 Buildah 相同的代碼進行構(gòu)建。但是,Buildah 的命令比 Podman 的命令詳細得多,允許對鏡像進行更細粒度的控制并允許創(chuàng)建更精細的鏡像層。Podman 的“構(gòu)建”命令使用了 Buildah 功能的一個子集。
Buildah 專注于構(gòu)建容器鏡像,復制在沒有守護程序套接字組件的 Dockerfile 中找到的所有命令,而 Podman 專注于維護和修改容器中的這些鏡像所需的東西。使用 Podman,您可以創(chuàng)建一個容器——使用 Buildah 提供容器鏡像——然后使用熟悉的命令行界面 (CLI) 命令(如果您可以運行一個Docker CLI 中的命令,您可以在 Podman CLI 中運行相同的命令)。
Podman 和 Buildah 的另一個不同之處是:Buildah 的容器主要是臨時創(chuàng)建的,以允許將內(nèi)容傳輸?shù)秸趧?chuàng)建的容器鏡像中,而使用 Podman,用戶創(chuàng)建傳統(tǒng)容器,旨在使用和維護更長時間. Buildah 的容器用于短期目的,而 Podman 的容器用于長期目的。
Buildah 和 Podman 各自創(chuàng)建的容器是互相看不到的。
4.安裝
4.1 CentOS
4.2 Ubuntu
4.3 RHEL7
4.4 Fedora
或者
- 更多安裝方式請參考這里[10]
5. 命令
Command | Description |
buildah-add(1)[11] | 將文件、URL 或目錄的內(nèi)容添加到容器中。 |
buildah-build(1)[12] | 使用 Containerfiles 或 Dockerfiles 中的指令構(gòu)建鏡像。 |
buildah-commit(1)[13] | 從運行的容器創(chuàng)建鏡像。 |
buildah-config(1)[14] | 更新鏡像配置設(shè)置。 |
buildah-containers(1)[15] | 列出工作容器及其基礎(chǔ)鏡像。 |
buildah-copy(1)[16] | 將文件、URL 或目錄的內(nèi)容復制到容器的工作目錄中。 |
buildah-from(1)[17] | 從頭開始或使用指定鏡像作為起點創(chuàng)建一個新的工作容器。 |
buildah-images(1)[18] | 列出本地存儲中的鏡像。 |
buildah-info(1)[19] | 顯示 Buildah 系統(tǒng)信息。 |
buildah-inspect(1)[20] | 檢查容器或鏡像的配置。 |
buildah-mount(1)[21] | 掛載工作容器的根文件系統(tǒng)。 |
buildah-pull(1)[22] | 從指定位置拉取鏡像。 |
buildah-push(1)[23] | 將鏡像從本地存儲推送到其他地方。 |
buildah-rename(1)[24] | 重命名本地容器 |
buildah-rm(1)[25] | 刪除一個或多個工作容器。 |
buildah-rmi(1)[26] | 刪除一個或多個鏡像. |
buildah-run(1)[27] | 在容器內(nèi)運行命令。 |
buildah-tag(1)[28] | 為本地鏡像添加一個額外的名稱。 |
buildah-umount(1)[29] | 卸載工作容器的根文件系統(tǒng)。 |
buildah-unshare(1)[30] | 在具有修改后的 ID 映射的用戶命名空間中啟動命令。 |
buildah-version(1)[31] | 顯示 Buildah 版本信息 |
6. 示例
配置別名
6.1 命令行構(gòu)建一個 httpd 鏡像
第一步是提取基本映像并創(chuàng)建工作容器
將包添加到工作容器
為Web服務器創(chuàng)建包含某些內(nèi)容的工作目錄:
將本地文件復制到工作容器
定義容器入口點以啟動應用程序
配置完成后,保存鏡像:
列出本地鏡像
現(xiàn)在可以使用podman在本地利用新生成的鏡像運行容器:
測試
要將映像推送到本地Docker倉庫,請執(zhí)行以下操作:
也可以這樣執(zhí)行:
Skopeo檢查結(jié)果
6.2 Dockerfile 構(gòu)建
按CTRL+X退出,按Y保存,按Enter退出nano
構(gòu)建
運行容器
測試訪問
6.3 構(gòu)建鏡像腳本(代替 Dockerfile)
- build_buildah_upstream.sh
構(gòu)建鏡像:
運行容器:
參考:
- Building Images With Buildah[32]
- Building with Buildah: Dockerfiles, command line, or scripts[33]
引用鏈接
[1] Buildah: https://buildah.io/
[2] 容器: https://www.redhat.com/en/topics/containers/whats-a-linux-container
[3] Docker: https://www.redhat.com/en/topics/containers/what-is-docker
[4] Kubernetes: https://www.redhat.com/en/topics/containers/what-is-kubernetes
[5] 容器倉庫: https://www.redhat.com/en/topics/cloud-native-apps/what-is-a-container-registry
[6] 運行時環(huán)境: https://www.redhat.com/en/topics/cloud-native-apps/what-is-a-Java-runtime-environment
[7] Dockerfiles: https://blog.csdn.net/xixihahalelehehe/article/details/107517710
[8] Docker Hub: https://hub.docker.com/
[9] Podman: https://podman.io/
[10] 更多安裝方式請參考這里: https://github.com/containers/buildah/blob/main/install.md
[11] buildah-add(1): /https://github.com/containers/buildah/blob/main/docs/buildah-add.1.md
[12] buildah-build(1): https://github.com/containers/buildah/blob/main/docs/buildah-build.1.md
[13] buildah-commit(1): https://github.com/containers/buildah/blob/main/docs/buildah-commit.1.md
[14] buildah-config(1): https://github.com/containers/buildah/blob/main/docs/buildah-config.1.md
[15] buildah-containers(1): https://github.com/containers/buildah/blob/main/docs/buildah-containers.1.md
[16] buildah-copy(1): https://github.com/containers/buildah/blob/main/docs/buildah-copy.1.md
[17] buildah-from(1): https://github.com/containers/buildah/blob/main/docs/buildah-from.1.md
[18] buildah-images(1): https://github.com/containers/buildah/blob/main/docs/buildah-images.1.md
[19] buildah-info(1): https://github.com/containers/buildah/blob/main/docs/buildah-info.1.md
[20] buildah-inspect(1): https://github.com/containers/buildah/blob/main/docs/buildah-inspect.1.md
[21] buildah-mount(1): https://github.com/containers/buildah/blob/main/docs/buildah-mount.1.md
[22] buildah-pull(1): https://github.com/containers/buildah/blob/main/docs/buildah-pull.1.md
[23] buildah-push(1): https://github.com/containers/buildah/blob/main/docs/buildah-push.1.md
[24] buildah-rename(1): https://github.com/containers/buildah/blob/main/docs/buildah-rename.1.md
[25] buildah-rm(1): https://github.com/containers/buildah/blob/main/docs/buildah-rm.1.md
[26] buildah-rmi(1): https://github.com/containers/buildah/blob/main/docs/buildah-rmi.1.md
[27] buildah-run(1): https://github.com/containers/buildah/blob/main/docs/buildah-run.1.md
[28] buildah-tag(1): https://github.com/containers/buildah/blob/main/docs/buildah-tag.1.md
[29] buildah-umount(1): https://github.com/containers/buildah/blob/main/docs/buildah-umount.1.md
[30] buildah-unshare(1): https://github.com/containers/buildah/blob/main/docs/buildah-unshare.1.md
[31] buildah-version(1): https://github.com/containers/buildah/blob/main/docs/buildah-version.1.md
[32] Building Images With Buildah: https://docs.oracle.com/en/operating-systems/oracle-linux/podman/podman-BuildingImagesWithBuildah.html#buildah-containers
[33] Building with Buildah: Dockerfiles, command line, or scripts: https://www.redhat.com/sysadmin/building-buildah