2

我正在尝试使用预定义的TARGETARCHarg变量通过 buildkit 创建一个多架构 docker 映像。

我想做的是 - 我认为 - 类似 bash 变量间接的东西,但我知道这不受支持,我正在努力想出一个替代方案。

这是我所拥有的:

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}

...这有点伪代码,但希望能说明我正在尝试做的事情:我想要 a 的值$DOWNLOAD_amd64$DOWNLOAD_arm64放入$DOWNLOAD_URL,具体取决于$TARGETARCH设置的内容。

这可能是一个长期解决的问题,但要么我在谷歌上搜索错误的东西,要么就是没有得到它。

4

1 回答 1

1

好的,不完整。这是一个完整的工作解决方案:

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"]

入口点.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

测试:

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

现在对于阿尔卑斯山:

Dockerfile

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

入口点.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

阿尔派不接受xargs -d。但这里并不那么有趣,因为 URL 不包含任何空白..

测试:Alpine 仅将其用于登录 shell .. 所以:

docker exec -it containername sh --login
echo $DOWNLOAD_URL
于 2021-12-13T15:47:40.210 回答