Building a Multi-Arch Swift Docker Image using Docker Desktop

Image for post
Image for post

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.

“Docker Desktop CE” can be found on Docker Hub.
If you follow the normal download links will not find the “Edge” version.

First go to Docker Hub create an account and login.
Next do a search for “desktop” and go to the “Docker CE” page.

Image for post
Image for post

Select the version for Mac or Windows. (The following will be for the Mac version)

Image for post
Image for post

Scroll down until you see the “Get Docker for Mac (Edge)” button.

Image for post
Image for post

Download and install. This should be version 2.1.0.0 or later.

Image for post
Image for post

Previously the “experimental features” were enabled by default but this is no longer true and will they need to be enabled.

Open “Preferences” (Settings)

Image for post
Image for post

Go to “>_ Command Line” , turn on “Enable experimental features” then “Apply & Restart”.

Image for post
Image for post

“Docker Desktop” is now ready to go.

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.

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.

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

Download and open the swiftlang-docker example repository.

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.

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

Image for post
Image for post
Image for post
Image for post

Pre-built images are available on Docker Hub.

Docker Instructions

Open a terminal on your server.

Image for post
Image for post
Swift on x86_64
Image for post
Image for post
Swift on aarch64/arm64
Image for post
Image for post
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.

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.

Get the Medium app