Browse topics

Building and publishing multi-platform images

Version
29
Review
Draft

Summary#

Give Buildx a comma-separated list of targets with --platform. With the default docker builder and the containerd image store, --load can save all the requested platforms locally. Use --push to publish a tag to a registry. The published image index identifies the image for each platform. Use docker buildx imagetools inspect to check what the registry serves.

Syntax#

sh
docker buildx build --platform linux/amd64,linux/arm64 --load --tag IMAGE:TAG .
docker image inspect --platform linux/arm64 IMAGE:TAG
docker buildx build --platform linux/amd64,linux/arm64 --push --tag REGISTRY/IMAGE:TAG .
docker buildx imagetools inspect REGISTRY/IMAGE:TAG

For files on the client, use --output type=local,dest=OUT with a build. This exports the result's filesystem into a directory.

Example#

Run this on a disposable Linux Docker daemon that uses the containerd image store. The CLI and daemon must share access to loopback, and the daemon must listen on the default Unix socket. Preload registry:3 or set REGISTRY_IMAGE to a local tag for that image. The build uses a scratch image with one text file. It fetches no base image and pushes only to the temporary loopback registry. Discard the daemon's storage after the example to clear the build cache.

sh
(
  set -eu
  unset DOCKER_HOST DOCKER_TLS DOCKER_TLS_VERIFY DOCKER_CERT_PATH
  export DOCKER_CONTEXT=default
  dir=$(mktemp -d)
  export BUILDX_CONFIG="$dir/buildx"
  suffix=$(printf '%s' "${dir##*.}" | LC_ALL=C tr '[:upper:]' '[:lower:]')
  name="dlr-p1-docker-commands-buildx-$suffix"
  local_tag="$name:local"
  remote_tag=
  registry_id=
  cleanup() {
    if [ -n "$registry_id" ]; then
      docker rm -f "$registry_id" >/dev/null 2>&1 || true
    fi
    docker image rm "$local_tag" ${remote_tag:+"$remote_tag"} >/dev/null 2>&1 || true
    rm -rf -- "$dir"
  }
  trap cleanup EXIT

  mkdir "$dir/context"
  printf 'FROM scratch\nCOPY payload.txt /payload.txt\n' > "$dir/context/Dockerfile"
  printf 'reference card\n' > "$dir/context/payload.txt"

  registry_id=$(docker create --pull never --name "$name" \
    --publish 127.0.0.1::5000 --tmpfs /var/lib/registry "${REGISTRY_IMAGE:-registry:3}")
  docker start "$registry_id" >/dev/null
  tries=0
  until docker exec "$registry_id" wget -qO /dev/null http://127.0.0.1:5000/v2/; do
    tries=$((tries + 1))
    [ "$tries" -lt 30 ]
    sleep 1
  done
  registry=$(docker port "$registry_id" 5000/tcp)
  remote_tag="$registry/$name:demo"

  targets=linux/amd64,linux/arm64
  docker buildx build --builder default --quiet --platform "$targets" \
    --provenance=false --load --tag "$local_tag" "$dir/context" >/dev/null
  for platform in linux/amd64 linux/arm64; do
    docker image inspect --platform "$platform" \
      --format 'local image: {{.Os}}/{{.Architecture}}' "$local_tag"
  done

  docker buildx build --builder default --quiet --platform "$targets" \
    --provenance=false --push --tag "$remote_tag" "$dir/context" >/dev/null
  platforms=$(docker buildx imagetools inspect --format \
    '{{range .Manifest.Manifests}}{{println .Platform.OS .Platform.Architecture}}{{end}}' "$remote_tag")
  printf 'registry platforms\n'
  printf '%s\n' "$platforms" | LC_ALL=C sort | tr ' ' '/'
)

Expected output#

Text
local image: linux/amd64
local image: linux/arm64
registry platforms
linux/amd64
linux/arm64

The registry port, tags and digests can vary. The example disables provenance to show just the two runnable platforms.

Common mistakes#

  • no matching manifest for linux/arm64 in the manifest list entries, with the requested platform in the message, means that the tag has no image for that platform. List what it serves with docker buildx imagetools inspect IMAGE:TAG. Then publish the missing platform, or request one that exists with --platform and run it on a daemon that can emulate it.

  • Verify a pushed tag with imagetools inspect. Inspecting the local image can show a different result from the one served by the registry.

  • A Dockerfile that executes RUN for another architecture needs a builder that can run that architecture, for example through emulation or a native build node. The scratch example only copies a file.

  • The classic image store cannot hold a multi-platform image. Use a builder such as one with the docker-container driver and push to a registry, or use the containerd image store with the default builder.

Version applicability#

Fresh Docker Engine 29 installs normally use the containerd image store. Engines upgraded from older releases can still use the classic image store. The example uses the default docker builder with containerd, the --provenance flag added in Buildx 0.10, and docker image inspect --platform, which requires Engine API 1.49 or later. It was checked with Docker Engine 29 and Buildx 0.39.0.

Sources#

Attribution
DevLookup contributors
License
CC-BY-4.0