我正在 C# .NET core (VS 2017) 中编写,使用 Linux 容器的构建,使用 docker-compose。
当我构建图像(或发布)时,由于 nuget 的使用,总是会引用互联网。
遇到的最后一个错误(我认为是 nuget 上的停机时间,但无论如何 - 任何停机时间都不应导致构建/发布异常。
/usr/share/dotnet/sdk/2.1.503/NuGet.targets(114,5): error : Unable to load the service index for source https://api.nuget.org/v3/index.json. ...
我想构建镜像,即使 nuget 已关闭,甚至没有互联网连接
yml 文件如下所示:
services:
myProj:
image: my_proj
build:
context: ./all_projects/base_solution/
dockerfile: myProj/Dockerfile
似乎“dotnetpublish...”命令调用从互联网恢复。
如果我使用 --no-restore 运行发布,代码不会编译,但我想从我自己的计算机预构建中恢复 nuget 包。
我该怎么做?没有互联网连接?为什么我应该依赖互联网在每次构建时恢复 nuget?
为什么我无法从我自己的预构建镜像中恢复 nuget 包,而且并非总是如此?!从 nuget 获取(实际上 nuget 包在我的代码中偶尔不会改变)。
我可以只从 nuget 复制文件夹,而不使用“COPY”命令吗?
我做了以下事情:
从使用所有 nuget 包的“通用”项目构建镜像。
在内置图像中添加新的其他图像的引用,如下所示:
创建一个新的通用图像: # 没有运行时。
FROM microsoft/dotnet:2.1-sdk AS build
WORKDIR /src
COPY TestDock/TestDock.csproj TestDock/
FROM build AS publish
RUN dotnet publish TestDock.csproj -c Release -o /app
在原始图像中: 来自 microsoft/dotnet:2.1-运行时 AS 基础 工作目录/应用程序
# FROM microsoft/dotnet:2.1-sdk AS build do:
FROM my_common_image AS build
WORKDIR /src
COPY TestDock/TestDock.csproj TestDock/
# Added the following. I tried to copy all, but this doesn't help.
# I persume I can copy part of the common build.
COPY --from=build /app /app
COPY --from=build /src /src
COPY --from=build /usr /usr
FROM build AS publish
RUN dotnet publish TestDock.csproj --no-restore --no-dependecies -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "TestDock.dll"]
我尝试添加中间图像,Dockerfile如下:
# This image is base image for all dockers (no need runtime)
# docker build . -f DockerFile_Common -t docker_common
FROM microsoft/dotnet:2.1-sdk AS build
COPY . .
WORKDIR /src/myProj
FROM build AS publish
RUN dotnet publish myProj.csproj -c Release -o /app
RUN dotnet pack /src/myProj.csproj -c Release -o /app
并在我的图像中使用它(我不想使用互联网构建),而不是:
FROM microsoft/dotnet:2.1-sdk AS build
我做到了:
FROM docker_common AS build
我还尝试在将workdir设置为解决方案文件夹时添加行:“RUN dotnetpublish...”,并标记“Microsoft Visual Studio Offline Packages”(取消标记默认的“nuget.org”)。
尽管如此,上述内容仍无法正确构建。
最佳答案
如果不使用互联网,您可以按照以下步骤操作。
需要在Visual Studio中发布,如下:
(在命令行中替代发布)。
dotnet publish myProj.csproj -c Release /p:PublishProfile=Properties\PublishProfiles\
<myprofile>.pubxml /p:PublishDir=<proj_folder>\bin\Release\netcoreapp2.1\publish
或者在构建后事件中放入如下行:
dotnet publish $(ProjectDir)$(ProjectName).csproj -c $(Configuration)
/p:PublishProfile=Properties\PublishProfiles\PublishProfile.pubxml
/p:PublishDir=$(TargetDir)publish --no-build
...在 Dockerfile 中,我刚刚需要从相关发布目录执行“COPY ...”!
FROM microsoft/dotnet:2.1-runtime
COPY MyProjectFolder/bin/Release/netcoreapp2.1/publish /app
WORKDIR /app
ENTRYPOINT ["dotnet", "myproject.dll"]
关于asp.net-core - C# 核心的 Docker 构建需要互联网连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54572923/