无法执行,因为找不到应用程序或未安装兼容的 .NET SDK

dav*_*era 16 docker .net-core

我使用 ASP.NET Core 5 创建了一个基本的 Rest API,我想用 docker 运行。该应用程序在 IIS Express 上运行良好。

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

在此输入图像描述

我还想创建一个 docker 容器来启动应用程序。

在项目文件夹中,我创建了一个包含多个文件的 Docker 文件夹。 这是我的 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" ]
Run Code Online (Sandbox Code Playgroud)

我还有一个 Build.docker 文件:

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}
Run Code Online (Sandbox Code Playgroud)

最后我有一个 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:
Run Code Online (Sandbox Code Playgroud)

最后,我启动以下命令行:

docker build -f Docker/App.dockerfile -t webapp:Debug --build-arg URL_PORT=7909 .
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

这个过程相当快。我还启动了这个命令行:

docker run --name webapp.test -p 5000:7909 -it webapp:Debug
Run Code Online (Sandbox Code Playgroud)

不幸的是,我收到一条错误消息。

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
Run Code Online (Sandbox Code Playgroud)

Oma*_*jid 28

您的构建 Dockerfile 使用 ASP.NET Core 运行时容器映像:

FROM mcr.microsoft.com/dotnet/aspnet:5.0
Run Code Online (Sandbox Code Playgroud)

该容器没有 SDK,只有运行时。当你构建它时,它失败了:

RUN dotnet restore
Run Code Online (Sandbox Code Playgroud)
Could not execute because the application was not found or a compatible .NET SDK is not installed. and The application 'restore' does not exist
Run Code Online (Sandbox Code Playgroud)

因为该dotnet restore命令是SDK命令。如果您只是使用运行时,则它不可用。

您应该使用 SDK 容器来构建 Dockerfile(而不是运行时 Dockerfile,这很好):

FROM mcr.microsoft.com/dotnet/sdk:5.0
Run Code Online (Sandbox Code Playgroud)


Alp*_*glu 20

如果您已经安装了 SDK 并且您有 x64 机器;

System Variables从>中删除以下项目Path

C:\Program Files (x86)\dotnet
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


Muh*_*mar 5

就我而言,.net SDK 安装在不同的位置。一个简单的 where.exe dotnetpowershell 返回以下内容 在此输入图像描述

系统环境变量中有 2 个条目,即上面的 x86 条目,因此它优先,所以我交换了这两个条目,万岁它开始工作了。特别感谢 https://www.hanselman.com/blog/dotnet-could-not-execute-because-the-application-was-not-found-or-a-known-net-sdk-is-not-installed

  • where.exe 实用程序非常有帮助。我很高兴得知此事。 (3认同)