Multi-architecture Development with Docker on Mac

Docker on Mac provides multi-architecture support, which makes it a game to work with containers of alternate CPU architectures like Power (ppc64le), Arm etc. on Mac.

Run the following commands in a terminal to see it in action.

docker run -it aarch64/busybox uname -a
[..snip..]
Linux 7b75658b0015 4.9.87-linuxkit-aufs #1 SMP Fri Mar 16 18:16:33 UTC 2018 aarch64 GNU/Linux

docker run -it ppc64le/busybox uname -a
[..snip..]
Linux c0929dcbd8e9 4.9.87-linuxkit-aufs #1 SMP Fri Mar 16 18:16:33 UTC 2018 ppc64le GNU/Linux

Multi-architecture support can be extremely handy when working with different architectures. Let’s see how you can leverage this in your development workflow seamlessly. Take a look at the following Dockerfile snippet:

ARG IMAGE=ubuntu:16.04
FROM $IMAGE

#

Let’s say you would like to do a quick build and test locally for both Intel, Arm 64 and Power, before committing any changes to source control.
For Power:

docker build --build-arg IMAGE=ppc64le/ubuntu:16.04 .

For Intel:

docker build --build-arg IMAGE=ubuntu:16.04 .

For ARM64:

docker build --build-arg IMAGE=aarch64/ubuntu:16.04 .

If you are using Docker on Mac with Kubernetes support, then you can deploy a non-Intel container image in Kubernetes for basic sanity testing before deploying to test or production multi-architecture Kubernetes cluster. Here is a quick example:

kubectl create -f https://raw.githubusercontent.com/bpradipt/examples/master/kubeyamls/busybox_ppc64le_pod.yaml
pod "busybox-ppc64le" created

kubectl get pods
NAME              READY     STATUS    RESTARTS   AGE
busybox-ppc64le   1/1       Running   0          38s

kubectl exec -it busybox-ppc64le sh
/ # uname -a
Linux busybox-ppc64le 4.9.87-linuxkit-aufs #1 SMP Fri Mar 16 18:16:33 UTC 2018 ppc64le GNU/Linux

Internals

This is made possible by two pieces of technology – Qemu and Linux binfmt_misc.
If you are interested in knowing more about how this is exactly done, please catch up my previous article – http://cloudgeekz.com/1352/powerpc-docker-images-on-intel.html

In short, Qemu is a CPU emulator, and binfmt_misc allows running of a non-native CPU architecture binary (eg. Power, Arm) on host CPU (eg. Intel).

Docker for Mac uses a Linux VM leveraging xhyve OSX virtualisation solution.

You can access the Linux VM using screen and check the binfmt_misc settings.
screen ~/Library/Containers/com.docker.docker/Data/com.docker.driver.amd64-linux/tty
CTRL+A CTRL+\ followed by “y” to exit

The following output is from my setup.

linuxkit-025000000001:~# ls -ltr /proc/sys/fs/binfmt_misc
total 0
-rw-r--r--    1 root     root             0 May  3 18:10 status
--w-------    1 root     root             0 May  3 18:10 register
-rw-r--r--    1 root     root             0 May  3 18:10 qemu-ppc64le
-rw-r--r--    1 root     root             0 May  3 18:10 qemu-arm
-rw-r--r--    1 root     root             0 May  3 18:10 qemu-aarch64

linuxkit-025000000001:~# cat /proc/sys/fs/binfmt_misc/qemu-ppc64le
enabled
interpreter /usr/bin/qemu-ppc64le
flags: OCF
offset 0
magic 7f454c4602010100000000000000000002001500
mask fffffffffffffffcfffffffffffffffffeffff00

This indicates that any Power (ppc64le) binaries will get executed using qemu-ppc64le.
You can see the linuxkit config for binmft here – https://github.com/linuxkit/linuxkit/blob/master/pkg/binfmt/Dockerfile

Hope you find this useful. Please share your experiences or any questions in the comments section.

Pradipta Kumar Banerjee

I'm a Cloud and Linux/ OpenSource enthusiast, with 16 years of industry experience at IBM. You can find more details about me here - Linkedin

You may also like...