0

I have a small .Net Core 7 MVC project that i'm trying to run on Docker. Actually, the running part works but it looks like the wwwroot folder is only partially published. I'm using npm and webpack to generate the javascript and css files necessary and they seem to be present because the url to the generated javascript files and css files work.

But the rest of the files in a wwwroot subfolder, like images, are not published. It's a PWA and the serviceworker works. A "offline.html" in the wwwroot folder works. So files directly in wwwroot are published, along with the "dist" folder generated by npm/webpack. But other subfolders are not published

Below you can find a bit of configuration. I got to this configuration by multiple tutorials so maybe there is a piece of config that contradicts another piece but i don't see it

My Dockerfile:

WORKDIR /app
EXPOSE 80
EXPOSE 443


FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
WORKDIR /src
COPY ["MyProject/MyProject.csproj", "MyProject/"]
RUN dotnet restore "MyProject/MyProject.csproj"
COPY . .

WORKDIR "/src/MyProject"
RUN dotnet build "MyProject.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "MyProject.csproj" -c Release -o /app/publish
WORKDIR /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "MyProject.dll"]

My csproj

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>net7.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
    <DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
    <IsPackable>false</IsPackable>
    <MpaRoot>npm\</MpaRoot>
    <WWWRoot>wwwroot\</WWWRoot>
    <DefaultItemExcludes>$(DefaultItemExcludes);$(MpaRoot)node_modules\**</DefaultItemExcludes>

  </PropertyGroup>

  <!--<ItemGroup>
    <Content Include="npm\src\css\site.css" />
  </ItemGroup>-->

  <ItemGroup>
    <PackageReference Include="Dapper" Version="2.0.123" />
    <PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="7.0.2" />
    <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="7.0.2" />
    <PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="7.0.2" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.2" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.2">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="7.0.3" />
  </ItemGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.SpaServices.Extensions" Version="7.0.2" />
  </ItemGroup>

  <ItemGroup>
    <!-- Don't publish the MPA source files, but do show them in the project files list -->
    <Content Remove="$(MpaRoot)**" />
    <None Remove="$(MpaRoot)**" />
    <None Include="$(MpaRoot)**" Exclude="$(MpaRoot)node_modules\**" />
  </ItemGroup>   
</Project>

In my program.cs I added app.UseStaticFiles() before app.UseRouting().

kenhas
  • 77
  • 1
  • 8

1 Answers1

0

i found a similar post before i posted my question but i just couldn't see the difference. Then i spotted the differences in the folders. This docker file doesn't use '/app/build' and 'app/release' in the build and publish commands

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

FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
WORKDIR /src
COPY ["MyProject/MyProject.csproj", "MyProject/"]
RUN dotnet restore "MyProject/MyProject.csproj"
COPY . ./

RUN dotnet build "/src/MyProject/MyProject.csproj" -c Release -o /app

FROM build AS publish
RUN dotnet publish "/src/MyProject/MyProject.csproj" -c Release -o /app

FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "MyProject.dll"]

Instead of copying to "build" and "publish" folders, everything is done inside the /app folder. I'm guessing the wwwroot isn't copied form one folder to the other at some point. But that is just guessing. I'm sure someone can explain it. So, in short. With this dockerfile everything works but I don't know why

kenhas
  • 77
  • 1
  • 8