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

2024-03-14

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

我还想创建一个 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" ]

我还有一个 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}

最后我有一个 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:

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

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

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

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

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

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

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

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

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

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

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

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

FROM mcr.microsoft.com/dotnet/sdk:5.0
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

随机推荐