17

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://learn.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
4
  • There is no build or publish step in your dockerfile. What is the Build.docker? Commented Apr 7, 2021 at 15:12
  • App.Dockerfile doesn't build anything, try the build with Build.Dockerfile.
    – Max
    Commented Apr 7, 2021 at 15:15
  • build too ... I have the same error message Could not execute because the application was not found or a compatible .NET SDK is not installed. and The application 'restore' does not exist.
    – davidvera
    Commented Apr 7, 2021 at 15:41
  • In my case, I had a typo in ENTRYPOINT's dll name.
    – A.Mokhtari
    Commented Aug 18, 2021 at 20:56

8 Answers 8

29

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
1
  • I thought i thanked you... 2 years later i thank you for your answer. :)
    – davidvera
    Commented Apr 16, 2023 at 13:12
20

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

3
  • 1
    This solution worked for me. The C:\Program Files (x86)\dotnet path came before C:\Program Files\dotnet so it was using the wrong dotnet.exe. After deleting the x86 path everything works normally now. Commented Mar 11, 2022 at 22:17
  • 1
    Thank you so much! I've tried to re-install .net 6 SDK, tried repairing visual studio, nothing helped but this.
    – madd
    Commented Jul 7, 2022 at 12:27
  • Thanks, I just deleted this directory and it fixed the problem. Commented Jul 21, 2023 at 20:26
5

In my case dot net SDKs were installed in different locations. A simple where.exe dotnet on powershell returned the following enter image description here

There were 2 entries in system environment variables i.e. The one with x86 was above so it was taking precedence, so I swapped both of them and hurray it started working. Special thanks to https://www.hanselman.com/blog/dotnet-could-not-execute-because-the-application-was-not-found-or-a-compatible-net-sdk-is-not-installed

1
  • 3
    That where.exe utility is very helpful. I'm was glad to learn about it. Commented Mar 11, 2022 at 22:18
0

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
    
0

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

0

In my case volume mounting is created this kind of problem.

volumes:
  - ./docker/caadminservice/certificate:/**CAAdminService**

CAAdminService is the root folder in container. We should not mount that directly with host machine. It created me this kind of problem

0

Try installing .Net extension through Vs code

https://code.visualstudio.com/docs/languages/dotnet

0

Similar to Alper Ebicoglu's answer, I found just deleting the rogue dotnet dir fixed the problem:

C:\Program Files (x86)\dotnet

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.