c# - 由于 docker 错误,无法将 .net 5.0 和 6.0 发布到 Azure 容器注册表

标签 c# azure docker docker-compose nuget

我已经创建了一个 Web API 项目并添加了 docker 文件。当我将其发布到 Azure 容器注册表时,我收到以下错误。

5.0 和 6.0 都会发生这种情况。我已经尝试过 VS2019 和 VS2022。

错误: C:\Program Files\dotnet\sdk\5.0.408\NuGet.targets(131,5): 错误:无法加载源 https://api.nuget.org/v3/index.json 的服务索引。 [C:\src\learndoc\learndoc.csproj] C:\Program Files\dotnet\sdk\5.0.408\NuGet.targets(131,5): 错误:不知道这样的主机。 (api.nuget.org:443) [C:\src\learndoc\learndoc.csproj] C:\Program Files\dotnet\sdk\5.0.408\NuGet.targets(131,5): 错误:不知道这样的主机。 [C:\src\learndoc\learndoc.csproj] 卸下中间容器 d8f9a4f724d7 命令“cmd/S/C dotnet Restore“learndoc/learndoc.csproj””返回非零代码:1 C:\Users\source\repos\learndocker\learndoc\Dockerfile(0,0):错误 CTC1014:Docker 命令失败,退出代码 1。

我可以直接使用浏览器访问nuget路径。我尝试从 appdata 以及 localdata 位置删除 nuget 缓存,但仍然没有成功。我还尝试过重新启动 Visual Studio 并重新创建 Docker 文件。

Docker 文件内容:

FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
WORKDIR /app
EXPOSE 80

FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /src
COPY \["learndoc/learndoc.csproj", "learndoc/"\]
RUN dotnet restore "learndoc/learndoc.csproj"
COPY . .
WORKDIR "/src/learndoc"
RUN dotnet build "learndoc.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "learndoc.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT \["dotnet", "learndoc.dll"\]

最佳答案

我尝试过 .net 5.0 和 6.0,并且能够在 docker 中发布。

在 Visual Studio 中,我们在创建 docker 文件时选择了操作系统为 Linux

需要安装以下 NuGet。

Microsoft.VisualStudio.Azure.Containers.Tools.Targets
Swashbuckle.AspNetCore 

enter image description here

在 5.0 版本的 Docker 文件中使用。

#See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging.

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["WebApplication7.csproj", "."]
RUN dotnet restore "./WebApplication7.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "WebApplication7.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "WebApplication7.csproj" -c Release -o /app/publish /p:UseAppHost=false

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "WebApplication7.dll"]

6.0 版本遵循相同的步骤。并能够在docker中发布。
对于端口 80 和 443

部署到 Docker 后。

enter image description here

从 Visual studio 2022 部署到 Docker Hub 后。

enter image description here

To avoid the error, check below points.

  1. 尝试访问 NuGet 包源 https://api.nuget.org/v3/index.json在您的网络浏览器中查看它是否可以访问。
  2. 如果无法访问 NuGet 包源,请尝试使用其他包源或稍后重试。
  3. 如果 NuGet 包源可访问,请尝试清除 NuGet 缓存并再次恢复包。
  4. 如果问题仍然存在,您可以尝试重新安装 .NET SDK 或将其更新到最新版本。

关于c# - 由于 docker 错误,无法将 .net 5.0 和 6.0 发布到 Azure 容器注册表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75752565/

相关文章:

github - 从Github生成Dockerfile自动构建-找不到标签

database - 如何在 Docker 中连接容器外的 mg_client?

C# 在清除/设置为 null 后重新填充字典

c# - 仅根据案例值调用方法的开关的替代方法

c# - 从另一台服务器 ASP.NET 下载文件时如何在网页中显示进度

c# - C# 中的新手 MVVM 尝试使用 Esc 关闭应用程序(处理键命令)

azure - 获取 CustomVisionErrorException : Operation returned an invalid status code "BadRequest"

docker - 在 Docker 终端 Windows 中更改目录

无法使用 APIM/VM 中的专用终结点访问 Azure 应用服务

c# - 获取每个应用程序的所有用户 Azure Active Directory .NET Web Api C#