我有被码头化的.net核心应用 在docker内部的compose文件下面有此容器的详细信息 从Windows cli docker-compose up 产生以下错误
Step 11/20 : RUN dotnet restore My.Api/My.Api.csproj
---> Running in bc9d0ee76a7e
/usr/share/dotnet/sdk/2.1.202/Sdks/Microsoft.NET.Sdk/build/Microsoft.NET.TargetFrameworkInference.targets(135,5): error : The current .NET SDK does not support targeting .NET Core 2.2. Either target .NET Core 2.0 or lower, or use a version of the .NET SDK that supports .NET Core 2.2. [/src/My.Api/My.Api.csproj]
ERROR: Service 'my.api' failed to build: The command '/bin/sh -c dotnet restore My.Api/My.Api.csproj' returned a non-zero code: 1
docker-compose.yml
version: '3.4'
networks:
frontend:
backend:
services:
my.api:
image: ${DOCKER_REGISTRY}myapi
build:
context: .
dockerfile: My.Api/Dockerfile
ports:
- "5000:80"
networks:
- backend
- frontend
depends_on:
- mssqlserver
mssqlserver:
image: "microsoft/mssql-server-linux:latest"
.....
Dockerfile
FROM microsoft/aspnetcore:2.0 AS base
WORKDIR /app
EXPOSE 80
FROM microsoft/aspnetcore-build:2.0 AS build
WORKDIR /src
COPY My.Api/
RUN dotnet restore My.Api.csproj
COPY . .
WORKDIR /src/My.Api
RUN dotnet build My.Api.csproj -c Release -o /app
FROM build AS publish
RUN dotnet publish My.Api.csproj -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "My.Api.dll"]
dotnet --version
2.2.105
更新:
当我将Dockerfile更改为使用microsoft / aspnetcore:2.2而不是 microsoft / aspnetcore:2.0我正在获取
Step 1/20 : FROM microsoft/aspnetcore:2.2 AS base
ERROR: Service 'my.api' failed to build: manifest for microsoft/aspnetcore:2.2 not found
答案 0 :(得分:1)
首先,重要一点:
对于Docker容器,Windows中安装的.NET Core的版本无关紧要。 Windows中dotnet --version
所说的内容都没有关系。泊坞窗容器是与Windows环境不同的环境。它实际上运行一个不同的用户空间(Linux,而不是Windows)。它可以安装一套完全不同的程序和版本。
现在,返回,错误显示:
error : The current .NET SDK does not support targeting .NET Core 2.2. Either target .NET Core 2.0 or lower, or use a version of the .NET SDK that supports .NET Core 2.2
此错误消息的最后一部分非常重要。它告诉您问题所在:您正在使用无法定位2.2的.NET Core SDK(在docker容器内部)。
(还记得通常在Windows中安装的.NET Core SDK版本与docker无关吗?)
那么您在docker容器中使用的是那个版本的SDK?再次查看Dockerfile
:
FROM microsoft/aspnetcore-build:2.0 AS build
您正在使用支持ASP.NET Core 2.0的.NET Core SDK。换句话说,您使用的是.NET Core SDK 2.0,它仅支持定位.NET Core 2.0。您需要切换到支持.NET Core 2.2的SDK。
将FROM microsoft/aspnetcore:2.0 AS base
更改为FROM microsoft/aspnetcore:2.2 AS base
,将FROM microsoft/aspnetcore-build:2.0 AS build
更改为FROM microsoft/aspnetcore-build:2.2 AS build
。
这将为您提供一个SDK,让您可以定位.NET Core 2.2。
编辑:嗯,对。我错过了什么。
Microsoft container images are described here。您需要使用microsoft/dotnet:2.2-sdk
来构建(FROM microsoft/dotnet:2.2-sdk AS build
,并使用microsoft/dotnet:2.2-aspnetcore-runtime
来运行(FROM microsoft/dotnet:2.2-aspnetcore-runtime as base
)。