skip to Main Content

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.

2

Answers


  1. Ok, was not complete. Here a full working solution:

    Dockerfile:

    FROM ubuntu:18.04
    
    ARG TARGETARCH
    
    ARG DOWNLOAD_amd64="x86_64"
    ARG DOWNLOAD_arm64="aarch64"
    WORKDIR /tmp
    ARG DOWNLOAD_URL_BASE="https://download.url/path/to/toolkit-"
    RUN touch .env; 
        if [ "$TARGETARCH" = "arm64" ]; then 
        export DOWNLOAD_URL=$(echo $DOWNLOAD_URL_BASE$DOWNLOAD_arm64) ; 
        elif [ "$TARGETARCH" = "amd64" ]; then 
        export DOWNLOAD_URL=$(echo $DOWNLOAD_URL_BASE$DOWNLOAD_amd64) ; 
        else 
        export DOWNLOAD_URL="" ; 
        fi; 
        echo DOWNLOAD_URL=$DOWNLOAD_URL > .env; 
        curl ... #ENVS JUST VALID IN THIS RUN!
     
    COPY ./entrypoint.sh ./entrypoint.sh
    ENTRYPOINT ["/bin/bash", "entrypoint.sh"]
    

    entrypoint.sh

    #!/bin/sh
    
    ENV_FILE=/tmp/.env
    if [ -f "$ENV_FILE" ]; then
        echo "export " $(grep -v '^#' $ENV_FILE | xargs -d 'n') >> /etc/bash.bashrc
        rm $ENV_FILE
    fi
    
    trap : TERM INT; sleep infinity & wait
    

    Test:

    # bash
    root@da1dd15acb64:/tmp# echo $DOWNLOAD_URL
    https://download.url/path/to/toolkit-aarch64
    

    Now for Alpine:

    Dockerfile

    FROM alpine:3.13
    RUN apk add --no-cache bash
    

    Entrypoint.sh

    ENV_FILE=/tmp/.env
    if [ -f "$ENV_FILE" ]; then
        echo "export " $(grep -v '^#' $ENV_FILE) >> /etc/profile.d/environ.sh
        rm $ENV_FILE
    fi
    

    Alpine does not accept xargs -d. But not that interesting here due to the fact URL does not contain any blank..

    Testing:
    Alpine just uses that for login shells.. so:

    docker exec -it containername sh --login
    echo $DOWNLOAD_URL
    
    Login or Signup to reply.
  2. I had a similar problem, and landed here for ideas to solve it. In the end, I used uname -p

    curl -sSL https://download.url/path/to/toolkit-$(uname -p) -o /tmp/toolkit-${TARGETARCH}
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search