Search code examples
c#docker.net-coredockerfile

Run Asp Core MVC on Docker - the static files / wwwroot files are not loading


When I start my container, the page starts without styles, scripts, images and other static files - calls to load them return 404.

I would like to say that when I access the app folder, the wwwroot folder exists.

I tested it in my Release folder (with dotnet command). But in Docker it does not work. Yes, I'm using UseStaticFiles in my Startup class.

My Dockerfile is simple, it just runs the app. But I previously built and published the app, and the wwwroot is there.

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

COPY bin/Release app/

ENTRYPOINT ["dotnet", "app/MyApp.SGC.Site.dll"]

wwwroot exists

The application runs, but there are many erros:

Errors

Can someone help me?


Solution

  • You must run the dotnet publish command so that all the required files are copied into your app folder.

    Your release folder only has the dll files for your application so when you copy it into the docker container the wwwroot files are not copied.

    If you create a new web app project and tick the Docker support option it will make a dockerfile for you. Create a sample project so you can see the recommended way to containerize your application.

    Here is an example that uses the publish command:

    FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
    WORKDIR /app
    EXPOSE 80
    EXPOSE 443
    
    FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
    WORKDIR /src
    COPY ["MyApp.SGC.Site/MyApp.SGC.Site.csproj", "MyApp.SGC.Site/"]
    RUN dotnet restore "MyApp.SGC.Site/MyApp.SGC.Site.csproj"
    COPY . .
    WORKDIR "/src/MyApp.SGC.Site"
    RUN dotnet build "MyApp.SGC.Site.csproj" -c Release -o /app/build
    
    FROM build AS publish
    # publish will copy wwwroot files as well
    RUN dotnet publish "MyApp.SGC.Site.csproj" -c Release -o /app/publish
    WORKDIR /app/publish
    
    FROM base AS final
    WORKDIR /app
    COPY --from=publish /app/publish .
    ENTRYPOINT ["dotnet", "MyApp.SGC.Site.dll"]