Search code examples
unixseddockerfile

Dockerfile - sed command not replacing ARG variable


I have the following dockerfile

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /WebAdmin
EXPOSE 80
EXPOSE 443
ARG API_GATEWAY_STAGE_NAME
RUN echo "API_GATEWAY_STAGE_NAME is $API_GATEWAY_STAGE_NAME" 

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["MyApp.Web.Admin/MyApp.Web.Admin.csproj", "MyApp.Web.Admin/"]
RUN dotnet restore "./MyApp.Web.Admin/./MyApp.Web.Admin.csproj"
COPY . .
WORKDIR "/src/MyApp.Web.Admin"
RUN sed -i "s/~\//\/${API_GATEWAY_STAGE_NAME}\//g" ./Views/Shared/_Layout.cshtml
RUN more ./Views/Shared/_Layout.cshtml
RUN dotnet build "./MyApp.Web.Admin.csproj" -c $BUILD_CONFIGURATION -o /WebAdmin/build

FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "./MyApp.Web.Admin.csproj" -c $BUILD_CONFIGURATION -o /WebAdmin/publish /p:UseAppHost=false

FROM base AS final
WORKDIR /WebAdmin
COPY --from=publish /WebAdmin/publish .
ENTRYPOINT ["dotnet", "MyApp.Web.Admin.dll"]

The sed command to replace all occurences of ~ with the ARG API_GATEWAY_STAGE_NAME does not work. The variable is never expanded out and we are just left with a blank where the API_GATEWAY_STAGE_NAME should be

I have tried multiple different variations and combinations of the sed command but I am so far missing the correct combination


Solution

  • Each FROM stage resets any build arguments.

    You need the ARG API_GATEWAY_STAGE_NAME in the FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build stage