建置多架構擴充功能
強烈建議您的擴充功能至少支援下列架構:
linux/amd64linux/arm64
Docker Desktop 會根據使用者的系統架構擷取擴充功能映像檔。如果擴充功能未提供與使用者系統架構相符的映像檔,Docker Desktop 將無法安裝該擴充功能。因此,使用者無法在 Docker Desktop 中執行此擴充功能。
為多種架構建置並推送映像檔
如果您是透過 docker extension init 指令建立擴充功能,則目錄根目錄下的 Makefile 會包含一個名為 push-extension 的目標 (target)。
您可以執行 make push-extension 來針對 linux/amd64 和 linux/arm64 平台建置您的擴充功能,並將其推送至 Docker Hub。
例如
$ make push-extension
或者,如果您是從空白目錄開始,請使用下方指令為多種架構建置您的擴充功能:
$ docker buildx build --push --platform=linux/amd64,linux/arm64 --tag=username/my-extension:0.0.1 .
接著,您可以使用 docker buildx imagetools 指令檢查映像檔資訊清單 (manifest),確認映像檔是否適用於這兩種架構。
$ docker buildx imagetools inspect username/my-extension:0.0.1
Name: docker.io/username/my-extension:0.0.1
MediaType: application/vnd.docker.distribution.manifest.list.v2+json
Digest: sha256:f3b552e65508d9203b46db507bb121f1b644e53a22f851185d8e53d873417c48
Manifests:
Name: docker.io/username/my-extension:0.0.1@sha256:71d7ecf3cd12d9a99e73ef448bf63ae12751fe3a436a007cb0969f0dc4184c8c
MediaType: application/vnd.docker.distribution.manifest.v2+json
Platform: linux/amd64
Name: docker.io/username/my-extension:0.0.1@sha256:5ba4ceea65579fdd1181dfa103cc437d8e19d87239683cf5040e633211387ccf
MediaType: application/vnd.docker.distribution.manifest.v2+json
Platform: linux/arm64
提示如果您在推送映像檔時遇到問題,請確保您已登入 Docker Hub。否則,請執行
docker login進行驗證。
如需更多資訊,請參閱多平台映像檔頁面。
新增多架構二進位檔案
如果您的擴充功能包含部署至主機的二進位檔案,那麼在針對多架構建置擴充功能時,確保這些檔案具備正確的架構非常重要。
目前,Docker 尚未提供在 metadata.json 檔案中明確指定每個架構對應多個二進位檔案的方式。不過,您可以在擴充功能的 Dockerfile 中,根據 TARGETARCH 新增特定架構的二進位檔案。
以下範例展示了一個將二進位檔案作為其運作一部分的擴充功能。該擴充功能需要在 Mac 和 Windows 上的 Docker Desktop 中執行。
在 Dockerfile 中,根據目標架構下載二進位檔案:
#syntax=docker/dockerfile:1.3-labs
FROM alpine AS dl
WORKDIR /tmp
RUN apk add --no-cache curl tar
ARG TARGETARCH
RUN <<EOT ash
mkdir -p /out/darwin
curl -fSsLo /out/darwin/kubectl "https://dl.k8s.io/release/$(curl -Ls https://dl.k8s.io/release/stable.txt)/bin/darwin/${TARGETARCH}/kubectl"
chmod a+x /out/darwin/kubectl
EOT
RUN <<EOT ash
if [ "amd64" = "$TARGETARCH" ]; then
mkdir -p /out/windows
curl -fSsLo /out/windows/kubectl.exe "https://dl.k8s.io/release/$(curl -Ls https://dl.k8s.io/release/stable.txt)/bin/windows/amd64/kubectl.exe"
fi
EOT
FROM alpine
LABEL org.opencontainers.image.title="example-extension" \
org.opencontainers.image.description="My Example Extension" \
org.opencontainers.image.vendor="Docker Inc." \
com.docker.desktop.extension.api.version=">= 0.3.3"
COPY --from=dl /out /在 metadata.json 檔案中,指定每個平台上每個二進位檔案的路徑:
{
"icon": "docker.svg",
"ui": {
"dashboard-tab": {
"title": "Example Extension",
"src": "index.html",
"root": "ui"
}
},
"host": {
"binaries": [
{
"darwin": [
{
"path": "/darwin/kubectl"
}
],
"windows": [
{
"path": "/windows/kubectl.exe"
}
]
}
]
}
}結果,當 TARGETARCH 等於:
arm64時,擷取的kubectl二進位檔案對應至arm64架構,並在最終階段複製到/darwin/kubectl。amd64時,會擷取兩個kubectl二進位檔案。一個用於 Darwin,另一個用於 Windows。它們分別在最終階段複製到/darwin/kubectl和/windows/kubectl.exe。
注意在這兩種情況下,Darwin 的二進位檔案目的地路徑皆為
darwin/kubectl。唯一的改變是所下載的特定架構二進位檔案。
安裝擴充功能時,擴充功能架構會將二進位檔案從擴充功能映像檔中的 /darwin/kubectl (針對 Darwin) 或 /windows/kubectl.exe (針對 Windows) 複製到使用者主機檔案系統中的特定位置。
我可以開發執行 Windows 容器的擴充功能嗎?
雖然 Docker Desktop for Windows、Mac 和 Linux 皆支援 Docker Extensions,但擴充功能架構僅支援 Linux 容器。因此,您在建置擴充功能映像檔時,必須將 linux 作為目標作業系統。