Home > OS >  Setting conditional variables in a Dockerfile
Setting conditional variables in a Dockerfile

Time:12-15

I'm trying to create a multi-architecture docker image via buildkit, using the predefined TARGETARCH arg variable.

What I want to do is - I think - something like bash variable indirection, but I understand that's not supported and I'm struggling to come up with an alternative.

Here's what I've got:

FROM alpine:latest

# Buildkit should populate this on build with e.g. "arm64" or "amd64"
ARG TARGETARCH

# Set some temp variables via ARG... :/
ARG DOWNLOAD_amd64="x86_64"
ARG DOWNLOAD_arm64="aarch64"

ARG DOWNLOAD_URL="https://download.url/path/to/toolkit-${DOWNLOAD_amd64}"

# DOWNLOAD_URL must also be set in container as ENV var.
ENV DOWNLOAD_URL $DOWNLOAD_URL

RUN echo "Installing Toolkit" && \
    curl -sSL ${DOWNLOAD_URL} -o /tmp/toolkit-${DOWNLOAD_amd64}

... which is a bit pseudo-code but hopefully illustrates what I'm trying to do: I want the value of either $DOWNLOAD_amd64 or $DOWNLOAD_arm64 dropped into $DOWNLOAD_URL, depending on what $TARGETARCH is set to.

This is probably a long-solved issue but either I'm googling the wrong stuff or just not getting it.

CodePudding user response:

ARG TARGETARCH 
RUN if [ "$TARGETARCH" = "arm64" ]; then \
    export DOWNLOAD_URL=$(echo $DOWNLOAD_arm64) ;
    elif [ "$TARGETARCH" = "amd64" ]; then \
    export DOWNLOAD_URL=$(echo $DOWNLOAD_amd64) ;
    else \
    export DOWNLOAD_URL="" ;
    fi
  • Related