Sometimes you need to run or build containers on a different architecture than you are using natively. While you can tap into buildx for building containers, running containers built for a different architecture other than yours requires Docker Desktop with its magic or the image itself needs to have been built in a specific way. This rarely happens.
Using Colima’s built in CPU architecture emulation it is possible to create a Colima instance, or profile, for either arm64 (aarch64) or amd64 (x86_64) on both types of Mac, the M series or an Intel series. This means M series Macs can run x86_64 containers and Intel Macs can run arm64 based images and the containers won’t be aware that they aren’t running on native hardware. Containers running under emulation will run more slowly than they would if run on native hardware but having the ability to run them at all is really useful at times.
Here is what you do to setup a Colima profile running a different CPU architecture. I’m starting with an M1 based system with no Colima profiles created. You can see the current profiles by running
colima list WARN No instance found. Run
colima startto create an instance. PROFILE STATUS ARCH CPUS MEMORY DISK RUNTIME ADDRESS
From here I can create a new profile and tell it to emulate x86_64 using
colima start --profile amd64 -a x86_64 -c 4 -m 6. This command will create a Colima profile called “amd64” using architecture x86_64, 4 CPU cores and 6GB of memory. This Colima profile will take some time to start and will not have Kubernetes enabled. Give this some time to start up and then check your available Docker contexts using
docker context ls. You will get output similar to this:
docker context ls
NAME TYPE DESCRIPTION DOCKER ENDPOINT KUBERNETES ENDPOINT ORCHESTRATOR
colima-amd64 * moby colima [profile=amd64] unix:///Users/dustin/.colima/amd64/docker.sock
default moby Current DOCKER_HOST based configuration unix:///var/run/docker.sock
From here I can run a container. I’ll start with alpine container by running
docker run --rm -ti alpine uname -a to check what architecture it is running under. You should get this in return if you are on an M series Mac –
Linux 526bf44161d6 5.15.68-0-virt #1-Alpine SMP Fri, 16 Sep 2022 06:29:31 +0000 x86_64 Linux. Of course, you can run any container you need that is maybe x86_64 only.
Next I am going to run Nginx as an x86_64 container by running
docker run --rm -tid -p 80:80 --name nginx_amd64 nginx. Once Nginx is running I will demonstrate how to connect to it from another container running on a different architecture. This is super useful if you are testing different pieces of software together but one isn’t available natively for your platform.
Now I’ll create a native instance of Colima using
colima start --profile arm64 -c 4 -m 6. When this completes,
docker context ls will now show a new context that it has switched to. Running
docker ps will also show there is nothing running. You can switch between contexts using
docker context use followed by the name of the context you want to use.
With the new context available I will start a copy of Alpine Linux again and add the curl package using
apk add curl. With curl available, running
curl host.docker.internal will show a response from Nginx!
Now that I am done testing I can remove the emulated profile using
colima delete amd64 and the profile will be removed and cleaned up. Easy.