How to Build and Run ARM Docker Images on x86 Hosts
Sometimes we need to build and run images for ARM architectures such as Raspberry Pi. However, many of us do not have ARM machines, or we need to build and run on x86 machines for CI purposes, or we just want to have some fun. Anyway, this is a simple tutorial about building and running ARM images on x86 host machines.
Build ARM Images on x86 Hosts
1. Install QEMU
If you have a Debian or Ubuntu system, you can install qemu and binfmt module with apt-get command.
apt-get update && apt-get install -y --no-install-recommends qemu-user-static binfmt-support
update-binfmts --enable qemu-arm
update-binfmts --display qemu-arm
Or you can install qemu from Github source code.
git clone git://git.qemu.org/qemu.git
cd qemu
./configure --target-list=arm-linux-user --static
make
Please make sure that the qemu-arm-static binary is under /usr/bin/ directory of your system. If not, copy it from post-compiled qemu file.
Register qemu-arm with binfmt module.
mount binfmt_misc -t binfmt_misc /proc/sys/fs/binfmt_misc
echo ':arm:M::\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x28\x00:\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff:/usr/bin/qemu-arm-static:' > /proc/sys/fs/binfmt_misc/register
2. Copy qemu-arm-static to Your Working Directory
You need a copy of qemu-arm-static in your working directory for further modification.
cp /usr/bin/qemu-arm-static /your/working/dir
3. Modify the Dockerfile
Add this line of code, which makes a copy of qemu-arm-static binary to your docker image, below the "FROM" instruction in you Dockerfile.
COPY qemu-arm-static /usr/bin/qemu-arm-static
4. Build Docker Image with the Modified Dockerfile
Now build your docker image with the modified Dockerfile.
docker build -t your-image-name .
Run ARM Containers on x86 Hosts
1. Run ARM Images with qemu-arm-static
You can run ARM docker images just by mounting the volume with qemu-arm-static binary.
docker run -it --name your-container-name -v /usr/bin/qemu-arm-static:/usr/bin/qemu-arm-static your-arm-image