microsoft / DockerTools

Tools For Docker, including Visual Studio Provisioning and Publishing
Other
175 stars 26 forks source link

Cannot execute because the application was not found or a compatible .NET SDK is not installed #360

Closed intermension closed 2 years ago

intermension commented 2 years ago

Created an asp net core web app using a visual studio template. It works in debug mode/ docker mode from Visual Studio. But after successfully publishing to a docker repo and then pulling and running it, the container just exits straight away with the following error message:

Could not execute because the application was not found or a compatible .NET SDK is not installed. Possible reasons for this include:

  • You intended to execute a .NET program: The application 'webcore_docker.exe' does not exist.
  • You intended to execute a .NET SDK command: It was not possible to find any installed .NET SDKs. Install a .NET SDK from: https://aka.ms/dotnet-download

This is the docker file:

` FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base WORKDIR /app EXPOSE 80

FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build WORKDIR /src COPY ["source/source/webcore_docker/webcore_docker.csproj", "source/source/webcore_docker/"] RUN dotnet restore "source/source/webcore_docker/webcore_docker.csproj" COPY . . WORKDIR "/src/source/source/webcore_docker" RUN dotnet build "webcore_docker.csproj" -c Release -o /app/build

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

FROM base AS final WORKDIR /app COPY --from=publish /app/publish . ENTRYPOINT ["dotnet", "webcore_docker.exe"] ` These typically abysmal error messages from ms products don't help at all. Is there any reason why it just cant say what is wrong? Why is it some compounded series of potential things - nothing specific just a series of maybes? Can't the code just check if its the application that's missing OR if its an SDK issue, OR something else and communicate that specifically? How can the code not know why it cant run?

NCarlsonMSFT commented 2 years ago

Your entrypoint should be

ENTRYPOINT ["dotnet", "webcore_docker.dll"]

--or--

ENTRYPOINT ["webcore_docker.exe"]

Based on the error message, I suspect webcore_docker.exe is not present, so I advise using the .dll entry point.

intermension commented 2 years ago

Both webcore_docker.exe and webcore_docker.dll are available in the image and changing the entry point to either doesn't affect the outcome. But again why is there any need to guess at it? Is it not as simple as

If (!File.Exists(entryPointPath)) { LogSpecificErrorMessage - Including the path it expected to use}

intermension commented 2 years ago

The resolution for this is inexplicable. This stack overflow article says that a Dockerized asp.net app cannot specify http://localhost:port. It must use http:*:port. Making this change in the web builder fixes the issue:

public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); webBuilder.UseUrls("http://*:80"); });

I've not seen this documented anywhere and it seems well down stream from what the error messages were indicating but sample projects are now running in Docker.

NCarlsonMSFT commented 2 years ago

@intermension the base image sets the environment variable ASPNETCORE_URLS to http://+:80, so unless you are overriding the defaults, it should just work. Also just to test, I tried overriding using "http://localhost:80" and I did not see the error you are reporting, I got the expected behavior or the app starting but not being reachable outside the container.

I'm still not sure what wasn't working for you, but I am glad that you were able to get unblocked.