Docker Build Architectures
Posted: 20 Nov 2020
Updated: 24 Sep 2022
Verified: 24 Sep 2022
As soon as I deployed my first application on my pi k3s cluster, my deployment got stuck in an immediate crash/back-off loop. This turned out to be because the docker images were originally built and tested on amd64 architecture, but I had just deployed them to arm64!
Luckily, you can build for multiple architectures from one machine using buildx.
~~~sh
# Shows builders installed
docker buildx ls
# Use builder that supports current platform
docker buildx use default
# Or create a new builder
docker buildx create --name mybuilder
docker buildx use mybuilder
# docker buildx build --platform linux/arm64 --tag <image_name>:<image_tag> --push .
$ docker buildx build --platform=linux/amd64,linux/arm64 --tag mehcoleman/blog:0.9.0 --push .
[+] Building 29.5s (10/11)
=> [internal] load .dockerignore
=> => transferring context: 2B
=> [internal] load build definition from Dockerfile
=> => transferring dockerfile: 155B
...
=> => pushing layers
33.5s => => pushing manifest for docker.io/mehcoleman/blog:0.9.0@sha256:e65b2147446d67df341956114b0efd2a0aa9c440cfb68750186ecad76e0397b3
=> [auth] mehcoleman/blog:pull,push token for registry-1.docker.io
$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
mehcoleman/blog 0.9.0 49cc3ba8fdee 20 seconds ago 256MB
The --push
option is very handy, but you can manually push this tagged build
to your docker image repo too:
$ docker tag blog:0.9.0 mehcoleman/blog:0.9.0
$ docker image push mehcoleman/blog:0.9.0
The push refers to repository [docker.io/mehcoleman/blog]
e3285c232a3c: Pushed
428dbf75d59a: Mounted from library/nginx
21ed097e7be7: Mounted from library/nginx
arm64: digest: sha256:df7d129a287dae6c156bbef2a0713d7c11be295f57e6c973f3684529af0b5257 size: 1782
Then, fix the broken deployment:
kubectl set image deployments/blog blog=mehcoleman/blog:0.9.0
deployment.apps/blog image updated
See Leverage multi-CPU architecture support for full details.