Skip to content

antony@notes:~/container$ cat "Podman-build-Multi-Arch-Images.md"

Podman build Multi-Arch Images

2026-01-28· container

Podman build Multi-Arch Images

  1. 撰寫 go 範例程式

    nano hello.go

    檔案內容如下:

    package main
    
    import (
            "fmt"
            "net/http"
    )
    
    func handler(w http.ResponseWriter, r *http.Request) {
            fmt.Fprintf(w, "Hello")
    }
    
    func main() {
            http.HandleFunc("/", handler)
            http.ListenAndServe(":8080", nil)
    }
  2. 編輯 Dockerfile

    nano Dockerfile

    檔案內容如下:

    FROM --platform=$BUILDPLATFORM docker.io/library/golang:1.22 AS builder
    
    ARG TARGETOS
    ARG TARGETARCH
    
    WORKDIR /app
    COPY . .
    
    # 利用 Go 的環境變數進行交叉編譯
    RUN GOOS=$TARGETOS GOARCH=$TARGETARCH go build -o /build/hello hello.go
    
    # 最後階段
    FROM scratch
    COPY --from=builder /build/hello /usr/local/bin/hello
    CMD ["/usr/local/bin/hello"]
  3. 初始化 image manifest

    podman manifest create hello

    執行結果:

    413534f46ca55438f717d734a4e088af1a92b003c6c837cf5753b6808161b125
  4. build image

    podman build --platform linux/amd64,linux/arm64 --manifest localhost/hello .

    執行結果:

    [linux/arm64] [1/2] STEP 1/6: FROM docker.io/library/golang:1.22 AS builder
    [linux/arm64] [1/2] STEP 2/6: ARG TARGETOS
    --> 6af98f118a47
    [linux/arm64] [1/2] STEP 3/6: ARG TARGETARCH
    --> de656f2a8f6b
    [linux/arm64] [1/2] STEP 4/6: WORKDIR /app
    --> 2e367870d334
    [linux/arm64] [1/2] STEP 5/6: COPY . .
    --> 6ad772c2ca91
    [linux/arm64] [1/2] STEP 6/6: RUN GOOS=$TARGETOS GOARCH=$TARGETARCH go build -o /build/hello hello.go
    --> 30515c7aaf86
    [linux/arm64] [2/2] STEP 1/3: FROM scratch
    [linux/arm64] [2/2] STEP 2/3: COPY --from=builder /build/hello /usr/local/bin/hello
    --> 4ac0f5942408
    [linux/arm64] [2/2] STEP 3/3: CMD ["/usr/local/bin/hello"]
    [linux/arm64] [2/2] COMMIT
    --> 6251d8b5a1f3
    [linux/amd64] [1/2] STEP 1/6: FROM docker.io/library/golang:1.22 AS builder
    6251d8b5a1f392aed47bc124a0bd3f2b7c18f3d4ba9681ea153e853bf5d23370
    [linux/amd64] [1/2] STEP 2/6: ARG TARGETOS
    --> Using cache 6af98f118a47e4fbbcaff838816b73e31d7f5bd72e4b546baf82b026fe424662
    --> 6af98f118a47
    [linux/amd64] [1/2] STEP 3/6: ARG TARGETARCH
    --> Using cache de656f2a8f6bc7fc0e3f97eaaff21df053837e3d0e1a97f659a8a0ca40e2bb4a
    --> de656f2a8f6b
    [linux/amd64] [1/2] STEP 4/6: WORKDIR /app
    --> Using cache 2e367870d3341f7f57dbf1a1924281e277a645ecb29f031a979ea6f656ef2c07
    --> 2e367870d334
    [linux/amd64] [1/2] STEP 5/6: COPY . .
    --> Using cache 6ad772c2ca9135caef7c1fbeb79c126b36638dccab3b8eac884642020f330b4a
    --> 6ad772c2ca91
    [linux/amd64] [1/2] STEP 6/6: RUN GOOS=$TARGETOS GOARCH=$TARGETARCH go build -o /build/hello hello.go
    --> afbe26b87b3c
    [linux/amd64] [2/2] STEP 1/3: FROM scratch
    [linux/amd64] [2/2] STEP 2/3: COPY --from=builder /build/hello /usr/local/bin/hello
    --> 6dcdcf58bf83
    [linux/amd64] [2/2] STEP 3/3: CMD ["/usr/local/bin/hello"]
    [linux/amd64] [2/2] COMMIT
    --> b6c0da75b480
    b6c0da75b480acb9283ba0978937fe3668490c17bd39739a02211b1079227752
  5. 檢視 image

    podman images localhost/hello:latest

    執行結果:

    REPOSITORY       TAG         IMAGE ID      CREATED         SIZE
    <none>           <none>      b6c0da75b480  20 minutes ago  7.07 MB
    localhost/hello  latest      413534f46ca5  25 minutes ago  1.04 kB
  6. 檢視 image manifest

    podman manifest inspect localhost/hello:latest

    執行結果:

    {
        "schemaVersion": 2,
        "mediaType": "application/vnd.docker.distribution.manifest.list.v2+json",
        "manifests": [
            {
                "mediaType": "application/vnd.oci.image.manifest.v1+json",
                "size": 498,
                "digest": "sha256:48c8c68bc197b35831750d260600d3eb6c127ac64463d8f26d4f369e801ec4dd",
                "platform": {
                    "architecture": "arm64",
                    "os": "linux"
                }
            },
            {
                "mediaType": "application/vnd.oci.image.manifest.v1+json",
                "size": 498,
                "digest": "sha256:27a5911345bd07fc3461092bb0b7c5fb52fed1e35f4dd27eaf0cf833e2c19860",
                "platform": {
                    "architecture": "amd64",
                    "os": "linux"
                }
            }
        ]
    }
  7. Push the manifest and included images

    podman manifest push localhost/hello:latest docker://quay.io/myuser/image:latest