在 DockerFile 的文件路径中插入字符串

Interpolating strings in a file path in a DockerFile

我有一个 Docker 文件,其开头如下:

ARG FILE_PATH

FROM mcr.microsoft.com/dotnet/aspnet:3.1 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/sdk:3.1 AS build
WORKDIR /src

COPY ["${FILE_PATH}/src/NuGet.config", "src/"]

我这样使用 azure-cli 调用它:

$pathToSrc = "$(Build.SourcesDirectory)/My folder"

az acr build --build-arg "FILE_PATH=$pathToSrc" ...

这总是失败并显示消息:

COPY failed: file not found in build context or excluded by .dockerignore: stat src/NuGet.config: file does not exist

我试过变体,例如:

COPY [$FILE_PATH/src/NuGet.config, "src/"]
COPY ["FILE_PATH/src/NuGet.config", "src/"]

az acr build --build-arg "FILE_PATH='$pathToSrc'" ...

但总是以相同的消息结束。

有没有办法做到这一点。我 运行 在 Azure-devops 管道中的托管代理上。任务是任务:AzureCLI@2 使用 PowerShell Core 脚本。

这可能是相关的:

...after every FROM statements all the ARGs gets collected and are no longer available. Be careful with multi-stage builds.

试试这个:

FROM mcr.microsoft.com/dotnet/aspnet:3.1 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/sdk:3.1 AS build
WORKDIR /src

ARG FILE_PATH

COPY ["${FILE_PATH}/src/NuGet.config", "src/"]