There is many tips building docker image, one of it is Case
and If
. Some of you may already familiar with this, this the condition we set to build image. Most of it is to download different binary url, like this:
Using CASE:
RUN case ${TARGETPLATFORM} in \
"linux/amd64") QBIT_ARCH=x86_64 ;; \
"linux/arm64") QBIT_ARCH=aarch64 ;; \
esac \
&& curl -L https://github.com/userdocs/qbittorrent-nox-static/releases/download/$QBVERSION/$QBIT_ARCH-qbittorrent-nox -o qbittorrent-nox && \
mv ./qbittorrent-nox $HOME/.local/bin/qbittorrent-nox;
Using IF:
RUN if [ "${TARGETARCH}" = "amd64" ]; then \
wget https://github.com/userdocs/qbittorrent-nox-static/releases/download/$QBVERSION/x86_64-cmake-qbittorrent-nox && \
mv ./x86_64-cmake-qbittorrent-nox /home/$CONTAINERUSER/.local/bin/qbittorrent-nox; \
fi;
RUN if [ "${TARGETARCH}" = "arm64" ]; then \
wget https://github.com/userdocs/qbittorrent-nox-static/releases/download/$QBVERSION/aarch64-cmake-qbittorrent-nox && \
mv ./aarch64-cmake-qbittorrent-nox /home/$CONTAINERUSER/.local/bin/qbittorrent-nox; \
fi;
The both sample it used to download binary for 2 different architecture and pack it to docker image, which one to use? It matter of preferences, some people like case
otherhand if
also likeable. But as we noticed about $TARGETARCH
and $TARGETPLATFORM
, what is it? Well it's a default parameter that passed when using docker buildx
. On docker buildx we already know that we need --platform=linux/amd64,linux/arm64
or so depends you want.
The difference is the result we are looking,
When using $TARGETPLATFORM
There is also linux/arm/v6
, linux/i386
but pretty rare nowadays as the application moving forward to 64bit instructions, the only most requested 32bit is linux/arm
because of raspberry pi, surprisingly it still promote 32bit.
When using $TARGETARCH
With that differences, you can't make a compare $TARGETARCH=linux/amd64
and vice versa, they got their own results.
The last part is how do we catch it? We modify our Dockerfile by adding ARG
. If we dont adding ARG it wont catch it even we passed with --platform
on building.
FROM debian:latest
ARG TARGETARCH
ARG TARGETPLATFORM
RUN this
RUN that
RUN if $TARGETARCH = bla
RUN case $TARGETPLATFORM = bla
sources: