Approaches to build and test PowerPC Docker Images on Intel System

If you are running a multi-architecture setup in test and production, whereas doing primary application development on your Intel laptop or cloud provider instances, then having a mechanism to functionally verify the application for multiple-architectures might come in handy.

In this blog, we’ll explore some of the available approaches to build and test PowerPC LE (ppc64le) Docker images on your Intel (x86_64) system running Linux. You’ll most likely want to use this as part of your development workflow to verify and test your application to ensure its multi-arch capable before deploying it in test/production.

We’ll see how to leverage Qemu and Linux binfmt_misc capability to build and test Docker images for PowerPC LE (ppc64le) architecture on Intel.

Qemu is an emulator and virtualizer and has two operating modes:

  • Full system emulation: In this mode, Qemu emulates a full system (for example a PC), including one or several processors and various peripherals. It can be used to launch a full-fledged Operating System built for one CPU architecture (eg. PowerPC LE) on another CPU architecture (eg. Intel).
  • User mode emulation: In this mode, Qemu can launch binaries compiled for one CPU architecture on another CPU architecture.

This article describes how to leverage Qemu user mode emulation. In a follow-up article, I’ll touch upon leveraging full-system emulation mode.

Initial Setup
The Linux system needs to be setup to use Qemu user mode emulation. For this, we need to register the appropriate Qemu binary as the interpreter/handler for any PowerPC LE binaries.

The following command sets the appropriate configuration:

# docker run --rm --privileged multiarch/qemu-user-static:register

After running the above command, you should see the following output on the host.

# cat /proc/sys/fs/binfmt_misc/ppc64le
enabled
interpreter /usr/bin/qemu-ppc64le-static
flags:
offset      0
magic       7f454c4602010100000000000000000002001500
mask        ffffffffffffff00fffffffffffffffffeffff00

The above indicates that any ppc64le binary (magic – 7f454c460….) will be executed with /usr/bin/qemu-ppc64le-static.

The magic string is the information encoded in the header of the binary. You can use ‘readelf’ command to check the header of the binary:

# readelf -h <ppc64le_binary>
ELF Header:
Magic:   7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00
Class:      ELF64
Data:       2's complement, little endian
Version:    1 (current)
[snip]

Running PowerPC LE Docker Image on Intel Linux
In order to execute a PowerPC LE (ppc64le) docker image on Intel, we need to ensure that the container image has the qemu-ppc64le-static executable under /usr/bin path. Since as per the binfmt_misc configuration, the interpreter for any ppc64le binary is /usr/bin/qemu-ppc64le-static.

The following steps describe how this can be easily done by leveraging Docker volume mount capability.

  • Download Qemu Binary
# wget https://github.com/multiarch/qemu-user-static/releases/download/v2.7.0/qemu-ppc64le-static.tar.gz
# tar zxvf qemu-ppc64le-static.tar.gz
  • Run PowerPC LE Container
# docker run -v ~/qemu-ppc64le-static:/usr/bin/qemu-ppc64le-static -it ppc64le/hello-world

You should see the “Hello from Docker on ppc64le!” message getting displayed.

Building a PowerLC LE Docker Image on Intel Linux
As should be evident by now, if you want to build a PowerPC LE (ppc64le) Docker image on Intel, then you need to have volume mounting enabled for the image build operation  to use the qemu-ppc64le-static binary.

Something like ‘docker run -v <options>’ is desired.

Unfortunately, the upstream ‘docker’ binary doesn’t yet provide the capability to mount volumes during image build operation. Discussions are ongoing and more details are available here – https://github.com/docker/docker/issues/14080

However, if you are using RedHat based Linux distributions like Fedora, CentOS or RHEL, then the ‘docker’ binary that is shipped with these distributions provide for build time volume mount option.

In the subsequent sections we’ll see how we can build an image for both these cases – using ‘docker’ binary from RedHat distributions and from upstream.

Scenario-1

The following steps shows how to build a PowerPC LE (ppc64le) Docker image on Intel host running Fedora 24. This uses the ‘docker’ binary that is shipped with Fedora and provides the volume mount option as part of the build operation.

  • Download sample Dockerfile to build
# git clone https://github.com/bpradipt/docker-mysql.git
# cd docker-mysql
  • Build the Docker image
# docker build -v ~/qemu-ppc64le-static:/usr/bin/qemu-ppc64le-static  -f Dockerfile.ppc64le -t ppc64le/mysql .

Scenario-2

If you are using the upstream ‘docker’ binary (which doesn’t allow volume mounts as part of build operation) then the only way to build Docker images for a different architecture is by having a base image with the required Qemu binary.

You can create the base image yourself by taking an existing base image and then adding the Qemu binary to create a new base image having the Qemu binary. Note that the base image build process has to be carried out on a system with same CPU architecture as the base image.

If you don’t want to create a base image, then you can leverage the multiarch images from Dockerhub which packages qemu-ppc64le-static along with the base image. More details on multiarch docker images is available here – https://eyskens.me/multiarch-docker-images/

The following steps demonstrate building and testing of PowerPC LE (ppc64le) Docker image on Intel host by leveraging the multiarch base image.

  • Download sample Dockerfile to build
# git clone https://github.com/bpradipt/docker-mysql.git
# cd docker-mysql
  • Check the Dockerfile content

The key is the multiarch base image used in the FROM directive

# cat Dockerfile.multiarch.ppc64le
FROM multiarch/ubuntu-core:ppc64el-xenial

RUN apt-get update -y
RUN DEBIAN_FRONTEND=noninteractive apt-get install -y mysql-server
ADD server.cnf /etc/mysql/conf.d/
ADD setup.sh /setup.sh
EXPOSE 3306
ENTRYPOINT ["/setup.sh"]
  • Build the Docker Image
# docker build -t ppc64le/mysql -f Dockerfile.multiarch.ppc64le .

Verification
Run the following command on your Intel system to verify the PowerPC LE image

# docker run –itd -v ~/qemu-ppc64le-static:/usr/bin/qemu-ppc64le-static --name mysql -e MYSQL_ROOT_PASSWORD=password -e MYSQL_USER=user -e MYSQL_PASSWORD=pass -e MYSQL_DB=test -P ppc64le/mysql
# docker ps
# docker exec –it  mysql uname –m
ppc64le

I have created few hands-on scenarios in katacoda on the same topic. Feel free to try them, and share your feedback 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...