I know it's possible to get the architecture of the Docker engine simply by running this command :
root@minus:~# docker info | grep Architecture
Architecture: armv7l
I have also tested it on other architecture and it is working well (aarch64
on arm64 and x86_64
for my Intel server).
My question is is there a special variable (usable like an argument) with the architecture available inside a Dockerfile. I know it possible to use an ARG
for that but that mean that it has to be set by the caller, I would like to have something more automatic.
I would use it to automatically download the right image for the architecture. I've searched quite a lot and did not found anything and I'd be happy if there is a way ;).
Thanks in advance.
If you build using BuildKit, there are some predefined ARGs you can use:
- TARGETPLATFORM - platform of the build result. Eg linux/amd64, linux/arm/v7, windows/amd64.
- TARGETOS - OS component of TARGETPLATFORM
- TARGETARCH - architecture component of TARGETPLATFORM
- TARGETVARIANT - variant component of TARGETPLATFORM
- BUILDPLATFORM - platform of the node performing the build.
- BUILDOS - OS component of BUILDPLATFORM
- BUILDARCH - OS component of BUILDPLATFORM
- BUILDVARIANT - OS component of BUILDPLATFORM
These are documented in "Automatic platform ARGs in the global scope" in the builder documentation.
To use BuildKit, you can enable it within your shell with a variable:
export DOCKER_BUILDKIT=1
And then build using docker build
(support for BuildKit using docker-compose is being worked on, likely in the next release).
There are examples of conditional code execution based on architecture in some of the official Docker images. The golang
Dockerfiles, for example, use the package manager to report architecture and then a case
to conditionally run statements, all in a single chain of commands from a RUN
. From golang 1.13:alpine:
RUN ...
apkArch="$(apk --print-arch)"; \
case "$apkArch" in \
armhf) export GOARM='6' ;; \
x86) export GO386='387' ;; \
esac; \
...
In the Debian releases, dpkg
is used instead of apk
:
dpkgArch="$(dpkg --print-architecture)"; \
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With