Docker鏡像多架構(gòu)構(gòu)建介紹
前言:
目前arm系統(tǒng)越來越常見,對鏡像的多架構(gòu)需求也越來越大。對于同一個鏡像,最簡單的辦法就是在amd64或arm機器上build后通過不同的tag進行區(qū)分,比如 nginx:v1-amd64 、 nginx:v1-arm64 ,但這種方式比較丑陋,而且沒有對應(yīng)架構(gòu)的機器用來構(gòu)建怎么辦?
目前最新的辦法就是使用buildx來進行構(gòu)建,不過這個特性目前默認是沒啟用的,需要在docker的配置文件中添加 "experimental": true 后重啟docker服務(wù)生效。
首先執(zhí)行下面的命令讓amd64的機器也可以構(gòu)建arm的鏡像:
docker run --rm --privileged tonistiigi/binfmt:latest --install all
然后創(chuàng)建一個新的build實例:
docker buildx create --use --name=mybuilder-cn --driver docker-container --driver-opt image=dockerpracticesig/buildkit:master
這樣準備工作就全都做好了。
接下來以一個kubebuilder命令創(chuàng)建的operator項目默認的Dockerfile為例:
# Build the manager binary FROM --platform=$TARGETPLATFORM golang:1.16 as builder ARG TARGETOS TARGETARCH WORKDIR /workspace # Copy the Go Modules manifests COPY go.mod go.mod COPY go.sum go.sum # cache deps before building and copying source so that we don't need to re-download as much # and so that source changes don't invalidate our downloaded layer RUN go mod download # Copy the go source COPY main.go main.go COPY api/ api/ COPY controllers/ controllers/ # Build RUN CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -a -o manager main.go # Use distroless as minimal base image to package the manager binary # Refer to https://github.com/GoogleContainerTools/distroless for more details FROM --platform=$TARGETPLATFORM gcr.io/distroless/static:nonroot WORKDIR / COPY --from=builder /workspace/manager . USER 65532:65532 ENTRYPOINT ["/manager"]
修改點有2個:
--platform=$TARGETPLATFORM GOOS=${TARGETOS} GOARCH=${TARGETARCH}
這些TARGET開頭的變量可以在參考鏈接2里看到全部含義。
接下來使用這個文件進行構(gòu)建:
docker buildx build -t hello/namespace/name:v1 -f Dockerfile . --platform linux/amd64,linux/arm64 --push
注意這里的 buildx 、 --platform 參數(shù)后面跟隨需要構(gòu)建的版本、以及 --push ,buildx構(gòu)建的多架構(gòu)鏡像要么使用這個參數(shù)push到鏡像倉庫,要么使用 --load 加載到本地,不可省略。
構(gòu)建完成后就會生成相應(yīng)的多架構(gòu)鏡像了,可以使用 docker manifest inspect 來進行驗證,比如:
docker manifest inspect hello/namespace/name:v1 { "schemaVersion": 2, "mediaType": "application/vnd.docker.distribution.manifest.list.v2+json", "manifests": [ { "mediaType": "application/vnd.docker.distribution.manifest.v2+json", "size": 1367, "digest": "sha256:a7b99854e13939e3beaf21c1da8b4654022018eda9f438155b18ae16eeff49a5", "platform": { "architecture": "amd64", "os": "linux" } }, { "mediaType": "application/vnd.docker.distribution.manifest.v2+json", "size": 2169, "digest": "sha256:844885928813685ffa8c5ea4c6e9e7a09d95ac79e417afae0be7b73086762cfd", "platform": { "architecture": "arm64", "os": "linux" } } ] }
可以看出確實是生成了多個架構(gòu)的鏡像,使用時直接在不同架構(gòu)的機器上pull就會自動下載對應(yīng)的架構(gòu)鏡像了?;蛘咭部梢允褂?docker pull --platform arm64|amd64 xxxxx 來指定拉取鏡像的架構(gòu)。
對于某些沒有辦法使用buildx的場景,我們可以手動build不同架構(gòu)的鏡像,然后再手動創(chuàng)建manifest,比如:
# 指定拉取amd64架構(gòu) docker pull --platform amd64 gcr.io/distroless/static:nonroot # 重新打tag docker tag 9ef34 hello/ns/static:nonroot-amd64 # 推送 docker push hello/ns/static:nonroot-amd64 # 指定拉取arm64架構(gòu) docker pull --platform arm64 gcr.io/distroless/static:nonroot # 重新打tag docker tag 91714 hello/ns/static:nonroot-arm64 # 推送 docker push hello/ns/static:nonroot-arm64 ## 制作manifest docker manifest create hello/ns/static:nonroot hello/ns/static:nonroot-amd64 hello/ns/static:nonroot-arm64 docker manifest push hello/ns/static:nonroot docker manifest rm hello/ns/static:nonroot
重點是最后3行,push manifest后使用就和第一種方法一樣了。
另外如果想進行多架構(gòu)構(gòu)建有幾個注意點:
yum|apt install
其他 查看鏡像倉庫里都有哪些鏡像:
curl -u "用戶名":"密碼" -X GET http://鏡像地址:5000/v2/_catalog?n=2000 | python -m json.tool
查看鏡像有哪些tag:
curl -u "用戶名":"密碼" -X GET http://鏡像地址:5000/v2/命名空間/鏡像名稱/tags/list | python -m json.tool
到此這篇關(guān)于Docker鏡像多架構(gòu)構(gòu)建介紹的文章就介紹到這了,更多相關(guān)Docker鏡像 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Docker 部署Go的兩種基礎(chǔ)鏡像的實現(xiàn)
這篇文章主要介紹了Docker 部署Go的兩種基礎(chǔ)鏡像的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-04-04Docker使用Dockerfile來創(chuàng)建鏡像
本篇文章主要介紹了Docker使用Dockerfile來創(chuàng)建鏡像,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-04-04docker安裝并持久化postgresql數(shù)據(jù)庫的操作步驟
這篇文章主要介紹了docker安裝并持久化postgresql數(shù)據(jù)庫的操作步驟,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12docker搭建redis三主三從集群的實現(xiàn)步驟
本文主要介紹了docker搭建redis三主三從集群,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-08-08如何修改Docke上傳/下載并發(fā)線程數(shù)來解決docker: unexpected EOF.錯誤問題
下載或上傳鏡像時,有可能因為網(wǎng)絡(luò)問題導致報出docker: unexpected EOF.的錯誤,這時候可以考慮通過修改上傳/下載并發(fā)線程數(shù)來解決,本文給大家分享兩種方法幫助大家解決,需要的朋友一起看看吧2023-03-03