c# - 不再在 docker 中使用 dotnet watch 的理由是什么

标签 c# visual-studio docker .net-core

大约一年前,我记得当我们想在 docker 中运行应用程序以进行开发时,我们使用 dotnet watch run 运行应用程序。但在最近的更新中,该模板正在创建一个发布版本并运行该版本。我同意这对生产有好处。但是为什么开发版就完全没有了呢?我搜索了很多但找不到为什么会发生这种变化。

像这样:

FROM microsoft/aspnetcore-build:2.0

# Required inside Docker, otherwise file-change events may not trigger
ENV DOTNET_USE_POLLING_FILE_WATCHER 1

# Set a working dir at least 2 deep. The output and intermediate output folders will be /code/obj and /code/bin
WORKDIR /code/app

# By copying these into the image when building it, we don't have to re-run restore everytime we launch a new container
COPY web.csproj .
COPY NuGet.config .
COPY Directory.Build.props .
RUN dotnet restore

# This will build and launch the server in a loop, restarting whenever a *.cs file changes
ENTRYPOINT dotnet watch run --no-restore

现在每次更改时,我们都需要发布应用程序以再次拥有一个工作的 docker。

我看到使用这种新方法在 visual studio 中调试工作正常,但我对 visual studio 如何能够附加到容器并进行远程调试感到困惑。更让我惊讶的是 visual studio 如何能够调试以 Release模式发布的应用程序?

但现在它看起来像这样:

FROM microsoft/dotnet:2.2-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM microsoft/dotnet:2.2-sdk AS build
WORKDIR /src
COPY ["MyProject.csproj", "MyProject"]
COPY ["MyProject.Common.csproj", "MyProject.Common"]
RUN dotnet restore "MyProject.csproj"
COPY . .
WORKDIR "/src/MyProject"
RUN dotnet build "MyProject.csproj" -c Release -o /app

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

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

最佳答案

声明明显但以防万一: dotnet watch 等待文件更改,然后重新编译,无需您手动重新启动。

仍然可以使用 dotnet watch,但在复制文件时在容器情况下没有任何优势。由于文件的复制发生在容器构建时,并将它们复制到镜像内的源位置,因此即使您使用 dotnet watch,对代码库的任何更改也不会在容器运行时反射(reflect)出来。

如果你想使用 dotnet watch,请考虑将源目录挂载为容器内的一个卷:)

您可以使用如下内容:

docker run --rm -it -p < port >:< port > -v ~/< sourcedirectory >:/< destination >/ -w /< destination >/aspnetapp mcr.microsoft.com/dotnet/core/sdk:3.0 dotnet watch run

如果不是很明显,-v 标志代表音量。如果您希望将卷添加到 Dockerfile,您可以阅读 Dockerfile & Volumes here .和 dotnet watch here .和 Docker volumes specifically here .最后我在我的历史记录中找到了 docker run command来自。

关于c# - 不再在 docker 中使用 dotnet watch 的理由是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56783758/

相关文章:

c# - 在 .NET 4.0 应用程序域上加载针对 .NET 4.5 的程序集

c# - 如何在另一个按钮的点击事件中访问一个按钮的发送者对象

vb.net - 将 Excel 电子表格另存为 PDF

c++ - mt.exe : general error c101008d: Failed to write the updated manifest to the resource of file . .. 访问被拒绝

visual-studio - 我可以将加载项添加到 Visual Studio 菜单的 "analyze"部分吗?

docker - 在 Docker 容器中运行的程序的二进制文件在哪里?

c# - 根据订单中的值(value)过滤列表?

node.js - 为什么 dotenv 没有加载到 docker 中,但在主机中运行良好?

docker - 无法启动具有持久卷的 Redis 容器

c# - NUnit 测试适配器显示重复的失败测试