Could not execute because the application was not found or a compatible .NET SDK is not installed

Viewed 28820

I created a basic Rest API using ASP.NET Core 5 i want to make run with docker. The application works fine on IIS Express.

https://docs.microsoft.com/fr-fr/aspnet/core/tutorials/first-web-api?view=aspnetcore-5.0&tabs=visual-studio

enter image description here

I also want to create a docker container in order to launch the application.

In the project folder i created a Docker folder with several files. Here is my App.dockerfile:

FROM mcr.microsoft.com/dotnet/aspnet:5.0

ARG WEBAPP_VERSION=0.0.1
LABEL maintainer=anymail@email_server.com \
    Name=webapp \
    Version=${WEBAPP_VERSION}
ARG URL_PORT
WORKDIR /app
ENV NUGET_XMLDOC_MODE skip
ENV ASPNETCORE_URLS http://*:${URL_PORT}
ENTRYPOINT [ "dotnet", "WebApplication.dll" ]

I also have a Build.docker file:

FROM mcr.microsoft.com/dotnet/aspnet:5.0
## Can be Debug or Release.
ARG BUILD_CONFIG=Debug
ARG BUILDER_VERSION=0.0.1
LABEL maintainer=some_email@email_server.com \
    Name=webapp-build-${BUILD_CONFIG} \
    Version=${BUILDER_VERSION}
## Will be the path mapped to the external volume.
ARG BUILD_LOCATION=/app/out
ENV NUGET_XMLDOC_MODE skip
WORKDIR /app
COPY *.csproj .
RUN dotnet restore
COPY . /app
RUN dotnet publish --output ${BUILD_LOCATION} --configuration ${BUILD_CONFIG}

Finally I have a docker-compose.yml

version: '3'
services:
  webapp:
    container_name: webapp.test
    image: webapp:${WEBAPP_VERSION}
    build:
      context: ../
      dockerfile: ./Docker/App.dockerfile
      args:
        WEBAPP_VERSION: ${WEBAPP_VERSION}
        URL_PORT: ${URL_PORT}
    ports:
      - "5000:${URL_PORT}"
    volumes:
      - appbuild:/app
    links:
      - mysql
    environment:
      MYSQL_SERVER_NAME: ${MYSQL_SERVER_NAME}
    env_file:
      - secrets.env
    depends_on:
      - builder
  
  builder:
    container_name: builder
    image: webapp:${BUILDER_VERSION}.${BUILD_CONFIG}
    build:
      context: ../
      dockerfile: ./Docker/Build.dockerfile
      args:
        BUILDER_VERSION: ${BUILDER_VERSION}
        BUILD_CONFIG: ${BUILD_CONFIG}
        BUILD_LOCATION: ${BUILD_LOCATION}
    volumes:
      - appbuild:${BUILD_LOCATION}
   
  mysql:
    container_name: ${MYSQL_SERVER_NAME}
    image: mysql/mysql-server:8.0.23
    restart: always
    volumes:
      - dbvol:/var/lib/mysql
    environment:
      MYSQL_RANDOM_ROOT_PASSWORD: root
    env_file:
      - secrets.env

volumes:
  appbuild:
  dbvol:

Finally, I launch the following command line :

docker build -f Docker/App.dockerfile -t webapp:Debug --build-arg URL_PORT=7909 .

enter image description here

the process is rather quick. I also launch this command line :

docker run --name webapp.test -p 5000:7909 -it webapp:Debug

I unfortunatelly obtain an 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 'WebApplication.dll' 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
5 Answers

Your build Dockerfile uses the ASP.NET Core runtime container image:

FROM mcr.microsoft.com/dotnet/aspnet:5.0

This container doesn't have an SDK, just the runtime. When you build it, it fails:

RUN dotnet restore
Could not execute because the application was not found or a compatible .NET SDK is not installed. and The application 'restore' does not exist

Because the dotnet restore command is an SDK command. It's not available if you are just using the runtime.

You should use the SDK container for your build Dockerfile (not your runtime Dockerfile, that's fine):

FROM mcr.microsoft.com/dotnet/sdk:5.0

If you already installed the SDK and you have x64 machine;

Delete the following item from System Variables > Path

C:\Program Files (x86)\dotnet

enter image description here

Had the same message while trying to configure Docker Container Debugging in VS Code for .NET 6. Couple of issues:

  1. In tasks.json of VS Code the default "target": "base" was not working for me, because I used stage build in Dockerfile. So I commented it out... But Docker extension needs it to inject the Debugging metadata. Changing to "target": "build" fixed the error.

  2. I skipped the EXPOSE and ENV part because it worked via docker compose or while manually forwarding ports in cmd. Should be at the beginning of base/build stage:

    FROM mcr.microsoft.com/dotnet/sdk:6.0-alpine as build
    WORKDIR /source
    EXPOSE 80                         #was missing
    ENV ASPNETCORE_URLS=http://+:80   #was missing
    

Works for me when i changed FROM mcr.microsoft.com/dotnet/core/aspnet:5.0 to: FROM mcr.microsoft.com/dotnet/sdk:5.0

Related