c# - 不再在 docker 中使用 dotnet watch 的理由是什么
问题描述
大约一年前,我记得当我们想在 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 如何能够调试以发布模式发布的应用程序?
但现在看起来像这样:
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,容器运行时也不会反映对代码库的任何更改。
如果您想使用 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 和卷的信息。和dotnet 在这里观看。和Docker 卷在这里。最后,我在历史记录中找到了docker run 命令的来源。
推荐阅读
- delphi - 递归复制目录并覆盖所有文件而无需用户确认
- reactjs - 根据 React-Select 中的条件恢复为默认的反应选择道具
- excel - Excel VBA IE 对象和使用下拉列表
- javascript - UnhandledPromiseRejectionWarning 在nodejs中出现类型错误
- domain-driven-design - DDD:使用领域事件来保证不变量的一致性是否正确?我有其他选择吗?
- reactjs - 如何在 reactJS 中正确创建可重用的 useLocation 函数
- javascript - 如果我不在控制台上使用 renderPage() 函数,为什么模式不会打开?
- javascript - 使用 select2 使用多维数组中的数据填充下拉列表
- sql - 如何在postgresql中选择多列值多次出现的所有行?
- php - 无法访问此站点。在 LAN 上提供 laravel 应用程序时