使用 Containerfile 建置容器

建立 Containerfile

如果目前沒有適合應用程式的容器,可以自行為裝置建置一個。

一般會從工作目錄建立映像檔,該目錄包含 Containerfile 與其他所需檔案。工作目錄也可以納入版本控制,以方便分享。

$ mkdir container-demo && cd container-demo

有許多使用 Containerfile 建置容器的範例。一個簡單的 Containerfile 通常會包含下列部分內容:

  • FROM 一行指定基礎或起始容器,例如最新版 Fedora 映像檔。如果本機尚未有該映像檔,系統會自動拉取。映像檔的指定方式和使用 podman pull 指令時相同。

  • 每個 RUN 指令都會建立一個分層。可在同一行使用 && 串接多個指令,盡量減少分層數量;同時加入 dnf clean all 等清理指令,以縮小最終映像檔大小。

  • 將工作目錄中的內容複製到容器。

  • 使用 EXPOSE 指定要監聽的連接埠。

  • 啟動應用程式。

    • CMD 可以被 podman run 指令覆寫。

    • ENTRYPOINT 通常用來指定基礎指令與預設選項,也可以搭配 CMD 提供其他選項。

範例:Web 應用程式

建立工作目錄,並放入一些 Web 伺服器內容:

$ mkdir demo-httpd && cd demo-httpd && echo 'sample container' > index.html

以 FROM 指令作為 Containerfile 開頭,用來指定基礎映像檔:

$ echo 'FROM fedora:latest' >> Containerfile

加入 RUN 指令以更新映像檔,並安裝所需應用程式與工具:

$ echo 'RUN dnf -y update && dnf -y install httpd git  && dnf clean all' >> Containerfile

上述範例會安裝 Git。如果 Web 內容存放在版本控制系統中,可以加入 RUN 敘述,將資料 clone 到容器;如果內容已位於建置工作目錄中,則可使用 COPY 指令將內容加入容器。

將範例 index.html 檔案複製到容器:

$ echo 'COPY index.html /var/www/html/index.html' >> Containerfile

EXPOSE 一行指定容器監聽的網路連接埠,podman run 指令的 --publish-all 選項會使用此資訊。

記錄可發布的連接埠:

$ echo 'EXPOSE 80' >> Containerfile

指定容器啟動時要執行的指令:

$ echo 'ENTRYPOINT /usr/sbin/httpd -DFOREGROUND' >> Containerfile
無 root 權限的容器目前尚不支援連接埠綁定。如果容器需要供網路存取,請在 root 命名空間中建置。Podman 1.1.0 搭配 slirp4netns v0.3.0 的上游測試版本已開始提供無 root 權限容器的連接埠綁定。

使用具描述性的標籤建置映像檔:

$ sudo podman build --tag fedora:myhttpd -f ./Containerfile

映像檔會出現在本機登錄檔:

$ sudo podman images
REPOSITORY                 TAG       IMAGE ID       CREATED         SIZE
localhost/fedora           myhttpd   223534b48a9c   3 minutes ago   474MB
docker.io/library/fedora   latest    8b38e3af7237   4 weeks ago     315MB

若要讓主機裝置可存取應用程式連接埠,請使用 --publish 或 -p 選項,並指定 hostPort:containerPort。也可以指定 IP 位址或連接埠範圍。更多選項請參閱 man page。

執行容器並發布連接埠:

$ sudo podman run -p 8080:80 --name myhttpd --rm fedora:myhttpd

檢視連接埠資訊:

$ sudo podman port myhttpd
80/tcp -> 0.0.0.0:8080

從主機裝置存取網頁:

$ curl localhost:8080

從遠端存取網頁時,請使用主機裝置的 IP 位址與已發布的連接埠號碼。

視需要開放防火牆連接埠、服務或來源。Fedora IoT 映像檔預設會透過 interfaces 選項允許同一網路上的任何來源:

$ sudo firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: eth0
  sources:
  services: dhcpv6-client mdns ssh
  ports:
  protocols:
  masquerade: no
  forward-ports:
  source-ports:
  icmp-blocks:
  rich rules:

使用下列指令新增連接埠:

$ sudo firewall-cmd --add-port 8080/tcp

關於 firewall-cmd 指令的更多資訊,請參閱 firewalld.org。

範例:與 GPIO 介面互動

若要與 GPIO 介面互動,可在現有映像檔上加入 libgpiod-utils 分層套件,或在容器中使用它。

若要加入套件分層:

$ sudo rpm-ostree install libgpiod-utils python3-libgpiod
$ sudo gpiodetect

若要為使用 GPIO 介面的應用程式建立容器,請在 root 命名空間中進行。

以 FROM 指令作為 Containerfile 開頭,用來指定基礎映像檔:

$ echo 'FROM fedora:latest' >> Containerfile

加入 RUN 指令以更新映像檔,並安裝所需應用程式與工具:

$ echo 'RUN dnf -y update && dnf -y install git libgpiod-utils python3-libgpiod && dnf clean all' >> Containerfile

fedora:latest 映像檔已包含 Bash,因此可以直接建置容器,不需要事先指定要啟動的應用程式或公開的連接埠;執行容器時再指定指令即可。

使用具描述性的標籤建置映像檔:

$ sudo podman build --tag fedora:gpio -f ./Containerfile

映像檔會出現在 root 命名空間的 localhost 登錄檔:

$ sudo podman images
REPOSITORY                 TAG      IMAGE ID       CREATED         SIZE
localhost/fedora           gpio     655abf78e6b9   4 minutes ago   542MB
docker.io/library/fedora   latest   8b38e3af7237   4 weeks ago     315MB

若要從容器存取主機的 GPIO 裝置,啟動容器時請使用 --device 選項:

$ sudo podman run -it --name demo-gpio --device=/dev/gpiochip0 localhost/fedora:gpio /bin/bash

確認可以看到 GPIO 裝置:

[root@167f31750fdb /]# gpiodetect
gpiochip0 [pinctrl-bcm2835] (54 lines)

現在容器已能存取該裝置,接下來可以繼續使用已安裝的工具,或加入其他應用程式。

2018 年 Fedora Magazine 文章中有 gpioset 的使用範例:https://fedoramagazine.org/turnon-led-fedora-iot/[如何使用 Fedora IoT 點亮 LED]

可修改 Containerfile 並重新建置容器,以自動化更多步驟。

映像檔不一定要以 Fedora 容器為基礎建置。下列 Containerfile 使用 Raspbian 映像檔,並 clone lightshowpi 專案:

$ cat Containerfile
FROM raspbian/stretch:latest
RUN apt-get -y update && apt-get -y install git-core && apt-get -y clean
WORKDIR /
RUN git clone https://togiles@bitbucket.org/togiles/lightshowpi.git && \
  cd lightshowpi && git fetch && git checkout stable

Docker 文件包含 Containerfile 最佳實務。

重複使用與分享容器

容器映像檔建立完成後,可將其上傳至登錄檔,再部署到多台裝置。

大多數登錄檔要求使用 'useraccount/description:tag' 的命名格式,而多數 pull 指令預設會尋找標記為 'latest' 的容器。單一映像檔可以有多個標籤,這些標籤可協助識別架構相容性與版本。

若要重新命名本機映像檔或新增標籤:

$ podman tag fedora:myhttpd testuser/fedora-myhttpd:latest
$ podman tag fedora:myhttpd quay.io/testuser/fedora-myhttpd:latest

兩個名稱都會出現在映像檔清單中,但它們的映像檔 ID 會相同:

$ podman images
REPOSITORY                               TAG       IMAGE ID       CREATED        SIZE
localhost/fedora                         myhttpd   d52cbe4136e8   24 hours ago   428 MB
localhost/testuser/fedora-myhttpd        latest    d52cbe4136e8   24 hours ago   428 MB
quay.io/testuser/fedora-myhttpd          latest    d52cbe4136e8   24 hours ago   428 MB
docker.io/library/fedora                 latest    26ffec5b4a8a   4 weeks ago    283 MB

接著可以使用 podman push imageID destination 將映像檔推送至登錄檔。

若要以 Docker 格式將映像檔匯出至本機目錄:

$ podman push quay.io/testuser/fedora-myhttpd dir:/tmp/fedora-myhttpd

更多匯出選項請參閱 podman-push man page。