SBFrancies
SBFrancies

Reputation: 4240

Interpolating strings in a file path in a DockerFile

I have a Docker file which starts like this:

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

I call it using the azure-cli like this:

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

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

This always fails with the message:

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

I have tried variations such as:

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

and

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

but always end up with the same message.

Is there a way to do this. I am running on a hosted agent in Azure-devops pipeline. The task is task: AzureCLI@2 using a PowerShell Core script.

Upvotes: 2

Views: 1499

Answers (1)

beatcracker
beatcracker

Reputation: 6920

This may be related: https://stackoverflow.com/a/56748289/4424236

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

Try this:

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

Upvotes: 2

Related Questions