Skip to content

Instantly share code, notes, and snippets.

Last active April 23, 2022 18:57
  • Star 5 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Building Multi-Architecture Docker Images With Buildx
# (c) 2020
# SPDX-License-Identifier: Apache-2.0 OR MIT
# This script tries to reregister QEMU's binfmt_misc handlers with the
# fix-binary (F) flag in order to be usable with 'docker buildx' to build
# multi-architecture images.
# For more information see:
function remove_binfmt() {
local arch="$1"; shift
local package="$1"; shift
update-binfmts \
--package "${package}" \
--remove "qemu-${arch}" "/usr/bin/qemu-${arch}-static"
function install_binfmt() {
local arch="$1"; shift
local package="$1"; shift
local interpreter="$1"; shift
local offset="$1"; shift
local magic="$1"; shift
local mask="$1"; shift
update-binfmts \
--package "${package}" \
--install "qemu-${arch}" "${interpreter}" \
--offset "${offset}" \
--magic "${magic}" \
--mask "${mask}" \
--credentials yes \
--fix-binary yes
function reregister_qemu_binfmt() {
# Reregister all qemu interpreters with fix-binary flag which makes them
# available inside containers and chroots, e.g. docker buildx works for
# architectures supported by qemu interpreters.
for file in /proc/sys/fs/binfmt_misc/qemu-*; do
package="$(head -1 "/var/lib/binfmts/qemu-${arch}")"
# Pull arguments from current registration.
eval $(awk ' \
/^(interpreter|offset|magic|mask)/ {printf "%s=\"%s\"\n",$1,$2}' \
# Convert to binary strings.
magic="$(echo $magic | sed 's/\(..\)/\\x\1/g')"
mask="$(echo $mask | sed 's/\(..\)/\\x\1/g')"
echo "Reregistering arch $arch"
remove_binfmt "${arch}" "${package}"
install_binfmt "${arch}" "${package}" "${interpreter}" \
"${offset}" "${magic}" "${mask}"
set -e
reregister_qemu_binfmt "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment