FROM {{ netCoreRuntimeBaseImage }} AS base
WORKDIR /app
{{#each ports}}
EXPOSE {{ . }}
{{/each}}

{{#if (eq platform '.NET: ASP.NET Core')}}
{{#if ports}}
ENV ASPNETCORE_URLS=http://+:{{ ports.[0] }}

{{/if}}
{{/if}}
{{#unless (isRootPort ports)}}
{{#if (eq netCorePlatformOS 'Linux')}}
{{#if netCoreBaseImageDefaultUser }}
USER {{ netCoreBaseImageDefaultUser }}
{{else}}
# Creates a non-root user with an explicit UID and adds permission to access the /app folder
# For more info, please refer to https://aka.ms/vscode-docker-dotnet-configure-containers
RUN adduser -u 5678 --disabled-password --gecos "" appuser && chown -R appuser /app
USER appuser

{{/if}}
{{/if}}
{{/unless}}
{{#if (eq netCorePlatformOS 'Windows')}}
FROM {{ netCoreSdkBaseImage }} AS build
{{else}}
FROM --platform=$BUILDPLATFORM {{ netCoreSdkBaseImage }} AS build
{{/if}}
ARG configuration=Release
WORKDIR /src
COPY ["{{ workspaceRelative . artifact }}", "{{ dirname (workspaceRelative . artifact) }}/"]
RUN dotnet restore "{{ workspaceRelative . artifact netCorePlatformOS }}"
COPY . .
WORKDIR "/src/{{ dirname (workspaceRelative . artifact 'Linux') 'Linux' }}"
RUN dotnet build "{{ basename artifact }}" -c $configuration -o /app/build

FROM build AS publish
ARG configuration=Release
RUN dotnet publish "{{ basename artifact }}" -c $configuration -o /app/publish /p:UseAppHost=false

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "{{ netCoreAssemblyName }}"]
