Building a Multi-Arch Swift Docker Image using Docker Desktop

Neil Jones
Jul 9 · 5 min read

Earlier this year Docker announced a partnership with Arm enabling developers to build applications for server, cloud, edge, and IoT environments seamlessly on the latest Arm architecture.
With the latest Docker Desktop (edge version) you can now quickly and easily build multi-arch docker images for x86_64, arm/v6, arm/v7 and arm64.

This now means you can build a single docker image that can be deployed on any x86_64 server, the new arm64 servers from Packet and AWS or even small armv7 IOT devices such as the Raspberry Pi.

Get Started by Installing Docker Desktop

Install the latest “Docker Desktop”, open preferences and switch to the “edge” version.

Open “About Docker Desktop”and you will now see the extra supported platforms.

Prerequisites

Before trying to build a multi-arch docker image we first need to make sure that everything we are trying to build is available for the platform and arch types that we want to build for.
This includes not only the platform and the primary content but also all the dependencies that need to be installed.

In this example we want to create a docker container for Swift on Linux Ubuntu 18.04 that will run on x86_64, arm64/aarch64 and armv7. This means we will need Swift install packages for x86_64, arm64/aarch64 and armv7.

Open Source Swift on Linux

The official Swift releases are only available for x86_64 and in “tar.gz” format which means we need to create .deb install packages and add the other arch types in an easy to use package repository. By using sudo apt install we can simplify the docker file.

Swift Package Repository

I have created a package repository and built all the Swift install packages needed for x86_64, arm64 and armv7 on Ubuntu 18.04 and 16.04.
There are 2 types of package available for use with Swift docker containers.
swiftlang-coreThis contains only the Swift core libraries, Foundation and LibDispatch.
swiftlang-devThis is the full Swift release with all Dev tools such as SPM, XCTest, LLDB etc.
In this example we will use the full swiftlang-dev package.

The Package Repository is available here ~ Swift-Arm/Docker

With Docker Desktop installed and the Swift repository ready we can start building our multi-arch images.

Using the Swiftlang-Docker Sample on GitHub

Docker Multi-Arch (x86_64 / AArch64 / Armv7) Image packaging for Swift.
https://github.com/futurejones/swiftlang-docker

Open a Terminal Window

Download and open the swiftlang-docker example repository.

The Docker File

The docker image will be build from Ubuntu 18.04. In line 25 we add the Swift install repository and in line 27 we add Swift to the container image. The repository will automatically select the correct arch type for all the the arch types we include in the docker build command.

Using the new “buildx” command in Docker Desktop

Run docker buildx --help to see available commands

The default docker builder does not support multi-arch so we will need to create a new builder and tell docker to use it.

Create a new builder and tell docker to use it.

Use the platform tag add the different arch types to the image -

platform = linux/amd64,linux/arm64,linux/arm/v7

Terminal output should like similar to this …

terminal output for buildx build

Testing the Docker Image

Pre-built images are available on Docker Hub.

Docker Instructions

Open a terminal on your server.

Testing on AWS x86_64 server ~ t2.large

Swift on x86_64

Testing on AWS arm server ~ a1.xlarge

Swift on aarch64/arm64

Testing on Raspberry Pi 4

Swift on armv7

The new Docker Desktop enables us to build a single docker image from a single docker file that is compatible with multiple systems and arch types.

Neil Jones

Written by

Software Engineer, Designer and creator of Swift-Lite. Bringing Swift to the Raspberry Pi and IOT. More info @ www.swift-lite.org, www.swift-arm.com.