Buildah 入門

Buildah 入門

編譯自: https://opensource.com/article/18/6/getting-started-buildah

Buildah 提供一種靈活、可腳本編程的方式,來使用你熟悉的工具創建精簡、高效的容器鏡像。

Buildah 是一個命令行工具,可以方便、快捷的構建與 開放容器標準 (Open Container Initiative)(OCI)兼容的容器鏡像,這意味著其構建的鏡像與 Docker 和 Kubernetes 兼容。該工具可作為 Docker 守護進程 docker build 命令(即使用傳統的 Dockerfile 構建鏡像)的一種 簡單(drop-in)替換,而且更加靈活,允許構建鏡像時使用你擅長的工具。Buildah 可以輕鬆與腳本集成並生成 流水線(pipeline),最好之處在於構建鏡像不再需要運行容器守護進程(LCTT 譯註:這裡主要是指 Docker 守護進程)。

docker build 的簡單替換

目前你可能使用 Dockerfile 和 docker build 命令構建鏡像,那麼你可以馬上使用 Buildah 進行替代。Buildah 的 build-using-dockerfile (或 bud)子命令與 docker build 基本等價,因此可以輕鬆的與已有腳本結合或構建流水線。

類似我的上一篇關於 Buildah 的 文章 ,我也將以使用源碼安裝 “GNU Hello” 為例進行說明,對應的 Dockerfile 文件如下:

FROM fedora:28

LABEL maintainer Chris Collins

RUN dnf install -y tar gzip gcc make \

&& dnf clean all

ADD http://ftpmirror.gnu.org/hello/hello-2.10.tar.gz /tmp/hello-2.10.tar.gz

RUN tar xvzf /tmp/hello-2.10.tar.gz -C /opt

WORKDIR /opt/hello-2.10

RUN ./configure

RUN make

RUN make install

RUN hello -v

ENTRYPOINT "/usr/local/bin/hello"

使用 Buildah 從 Dockerfile 構建鏡像也很簡單,使用 buildah bud -t hello . 替換 docker build -t hello . 即可:

[chris@krang] $ sudo buildah bud -t hello .

STEP 1: FROM fedora:28

Getting image source signatures

Copying blob sha256:e06fd16225608e5b92ebe226185edb7422c3f581755deadf1312c6b14041fe73

81.48 MiB / 81.48 MiB [====================================================] 8s

Copying config sha256:30190780b56e33521971b0213810005a69051d720b73154c6e473c1a07ebd609

2.29 KiB / 2.29 KiB [======================================================] 0s

Writing manifest to image destination

Storing signatures

STEP 2: LABEL maintainer Chris Collins

STEP 3: RUN dnf install -y tar gzip gcc make && dnf clean all

鏡像構建完畢後,可以使用 buildah images 命令查看這個新鏡像:

[chris@krang] $ sudo buildah images

IMAGE ID IMAGE NAME CREATED AT SIZE

30190780b56e docker.io/library/fedora:28 Mar 7, 2018 16:53 247 MB

6d54bef73e63 docker.io/library/hello:latest May 3, 2018 15:24 391.8 MB

新鏡像的標籤為 hello:latest,我們可以將其推送至遠程鏡像倉庫,可以使用 CRI-O 或其它 Kubernetes CRI 兼容的運行時來運行該鏡像,也可以推送到遠程倉庫。如果你要測試對 Docker build 命令的替代性,你可以將鏡像拷貝至 docker 守護進程的本地鏡像存儲中,這樣 Docker 也可以使用該鏡像。使用 buildah push 可以很容易的完成推送操作:

[chris@krang] $ sudo buildah push hello:latest docker-daemon:hello:latest

Getting image source signatures

Copying blob sha256:72fcdba8cff9f105a61370d930d7f184702eeea634ac986da0105d8422a17028

247.02 MiB / 247.02 MiB [==================================================] 2s

Copying blob sha256:e567905cf805891b514af250400cc75db3cb47d61219750e0db047c5308bd916

144.75 MiB / 144.75 MiB [==================================================] 1s

Copying config sha256:6d54bef73e638f2e2dd8b7bf1c4dfa26e7ed1188f1113ee787893e23151ff3ff

1.59 KiB / 1.59 KiB [======================================================] 0s

Writing manifest to image destination

Storing signatures

[chris@krang] $ sudo docker images | head -n2

REPOSITORY TAG IMAGE ID CREATED SIZE

docker.io/hello latest 6d54bef73e63 2 minutes ago 398 MB

[chris@krang] $ sudo docker run -t hello:latest

Hello, world!

若干差異

與 Docker build 不同,Buildah 不會自動的將 Dockerfile 中的每條指令產生的變更提到新的 分層(layer)中,只是簡單的每次從頭到尾執行構建。類似於 自動化(automation)和 流水線構建(build pipeline),這種 無緩存構建(non-cached)方式的好處是可以提高構建速度,在指令較多時尤為明顯。從 自動部署(automated deployment)或 持續交付(continuous delivery)的視角來看,使用這種方式可以快速的將新變更落實到生產環境中。

但從實際角度出發,緩存機制的缺乏對鏡像開發不利,畢竟緩存層可以避免一遍遍的執行構建,從而顯著的節省時間。自動分層只在 build-using-dockerfile 命令中生效。但我們在下面會看到,Buildah 原生命令允許我們選擇將變更提交到硬盤的時間,提高了開發的靈活性。

Buildah 原生命令

Buildah 真正 有趣之處在於它的原生命令,你可以在容器構建過程中使用這些命令進行交互。相比與使用 build-using-dockerfile/bud 命令執行每次構建,Buildah 提供命令讓你可以與構建過程中的臨時容器進行交互。(Docker 也使用臨時或 中間 (intermediate)容器,但你無法在鏡像構建過程中與其交互。)

還是使用 “GNU Hello” 為例,考慮使用如下 Buildah 命令構建的鏡像:

#!/usr/bin/env bash

set -o errexit

# Create a container

container=$(buildah from fedora:28)

# Labels are part of the "buildah config" command

buildah config --label maintainer="Chris Collins " $container

# Grab the source code outside of the container

curl -sSL http://ftpmirror.gnu.org/hello/hello-2.10.tar.gz -o hello-2.10.tar.gz

buildah copy $container hello-2.10.tar.gz /tmp/hello-2.10.tar.gz

buildah run $container dnf install -y tar gzip gcc make

buildah run $container dnf clean all

buildah run $container tar xvzf /tmp/hello-2.10.tar.gz -C /opt

# Workingdir is also a "buildah config" command

buildah config --workingdir /opt/hello-2.10 $container

buildah run $container ./configure

buildah run $container make

buildah run $container make install

buildah run $container hello -v

# Entrypoint, too, is a “buildah config” command

buildah config --entrypoint /usr/local/bin/hello $container

# Finally saves the running container to an image

buildah commit --format docker $container hello:latest

我們可以一眼看出這是一個 Bash 腳本而不是 Dockerfile。基於 Buildah 的原生命令,可以輕易的使用任何腳本語言或你擅長的自動化工具編寫腳本。形式可以是 makefile、Python 腳本或其它你擅長的類型。

這個腳本做了哪些工作呢?首先,Buildah 命令 container=$(buildah from fedora:28) 基於 fedora:28 鏡像創建了一個正在運行的容器,將容器名(buildah from 命令的返回值)保存到變量中,便於後續使用。後續所有命令都是有 $container 變量指明需要操作的容器。這些命令的功能大多可以從名稱看出:buildah copy 將文件拷貝至容器,buildah run 會在容器中執行命令。可以很容易的將上述命令與 Dockerfile 中的指令對應起來。

最後一條命令 buildah commit 將容器提交到硬盤上的鏡像中。當不使用 Dockerfile 而是使用 Buildah 命令構建鏡像時,你可以使用 commit 命令決定何時保存變更。在上例中,所有的變更是一起提交的;但也可以增加中間提交,讓你可以選擇作為起點的 緩存點(cache point)。(例如,執行完 dnf install 命令後將變更緩存到硬盤是特別有意義的,一方面因為該操作耗時較長,另一方面每次執行的結果也確實相同。)

掛載點,安裝目錄以及 chroot

另一個可以大大增加構建鏡像靈活性的 Buildah 命令是 buildah mount,可以將容器的根目錄掛載到你主機的一個掛載點上。例如:

[chris@krang] $ container=$(sudo buildah from fedora:28)

[chris@krang] $ mountpoint=$(sudo buildah mount ${container})

[chris@krang] $ echo $mountpoint

/var/lib/containers/storage/overlay2/463eda71ec74713d8cebbe41ee07da5f6df41c636f65139a7bd17b24a0e845e3/merged

[chris@krang] $ cat ${mountpoint}/etc/redhat-release

Fedora release 28 (Twenty Eight)

[chris@krang] $ ls ${mountpoint}

bin dev home lib64 media opt root sbin sys usr

boot etc lib lost+found mnt proc run srv tmp var

這太棒了,你可以通過與掛載點交互對容器鏡像進行修改。這允許你使用主機上的工具進行構建和安裝軟件,不用將這些構建工具打包到容器鏡像本身中。例如,在我們上面的 Bash 腳本中,我們需要安裝 tar、Gzip、GCC 和 make,在容器內編譯 “GNU Hello”。如果使用掛載點,我仍使用同樣的工具進行構建,但下載的壓縮包和 tar、Gzip 等 RPM 包都在主機而不是容器和生成的鏡像內:

#!/usr/bin/env bash

set -o errexit

# Create a container

container=$(buildah from fedora:28)

mountpoint=$(buildah mount $container)

buildah config --label maintainer="Chris Collins

" $container

curl -sSL http://ftpmirror.gnu.org/hello/hello-2.10.tar.gz \

-o /tmp/hello-2.10.tar.gz

tar xvzf src/hello-2.10.tar.gz -C ${mountpoint}/opt

pushd ${mountpoint}/opt/hello-2.10

./configure

make

make install DESTDIR=${mountpoint}

popd

chroot $mountpoint bash -c "/usr/local/bin/hello -v"

buildah config --entrypoint "/usr/local/bin/hello" $container

buildah commit --format docker $container hello

buildah unmount $container

在上述腳本中,需要提到如下幾點:

  1. curl 命令將壓縮包下載到主機中,而不是鏡像中;
  2. (主機中的) tar 命令將壓縮包中的源代碼解壓到容器的 /opt 目錄;
  3. configure,make 和 make install 命令都在主機的掛載點目錄中執行,而不是在容器內;
  4. 這裡的 chroot 命令用於將掛載點本身當作根路徑並測試 "hello" 是否正常工作;類似於前面例子中用到的 buildah run 命令。

這個腳本更加短小,使用大多數 Linux 愛好者都很熟悉的工具,最後生成的鏡像也更小(沒有 tar 包,沒有額外的軟件包等)。你甚至可以使用主機系統上的包管理器為容器安裝軟件。例如,(出於某種原因)你希望安裝 GNU Hello 的同時在容器中安裝 NGINX :

[chris@krang] $ mountpoint=$(sudo buildah mount ${container})

[chris@krang] $ sudo dnf install nginx --installroot $mountpoint

[chris@krang] $ sudo chroot $mountpoint nginx -v

nginx version: nginx/1.12.1

在上面的例子中,DNF 使用 --installroot 參數將 NGINX 安裝到容器中,可以通過 chroot 進行校驗。

快來試試吧!

Buildah 是一種輕量級、靈活的容器鏡像構建方法,不需要在主機上運行完整的 Docker 守護進程。除了提供基於 Dockerfile 構建容器的開箱即用支持,Buildah 還可以很容易的與腳本或你喜歡的構建工具相結合,特別是可以使用主機上已有的工具構建容器鏡像。Buildah 生成的容器體積更小,更便於網絡傳輸,佔用更小的存儲空間,而且潛在的受攻擊面更小。快來試試吧!

[閱讀相關的故事, 使用 Buildah 創建小體積的容器 ]


via: https://opensource.com/article/18/6/getting-started-buildah

作者: Chris Collins 選題: lujun9972 譯者: pinewall 校對: wxy

本文由 LCTT 原創編譯, Linux中國 榮譽推出


分享到:


相關文章: