4

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.

Wintermute
  • 2,973
  • 4
  • 32
  • 52

2 Answers2

2

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
araisch
  • 1,727
  • 4
  • 15
  • the exported `DOWNLOAD_URL` will not propagate to subsequent `RUN` instructions. – jkr Dec 13 '21 at 15:50
  • Yeah, this could be used to do the installation step, i.e. wrap it around the `curl` command, but how do I set $DOWNLOAD_URL as an ENV var in the built image? – Wintermute Dec 13 '21 at 16:17
  • @Wintermute: I would create some .env file via the same RUN statement and load that in entrypoint. – Florin C. Dec 13 '21 at 17:18
  • Once you've run `curl` in the image, you can unpack it to a known architecture-independent path (`/opt/toolkit` or `/usr/local`) at which point you shouldn't need to know the original URL any more. – David Maze Dec 13 '21 at 17:20
  • Ok, thanks for the replies everyone. I guess the above is the most elegant way available right now, so that's what I'll go with :) – Wintermute Dec 14 '21 at 08:53
  • you could have just used `DOWNLOAD_URL=${DOWNLOAD_URL_BASE}$(uname -m)` – SantaXL Jul 02 '22 at 10:09
0

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}
Topher
  • 76
  • 6