如何使用"dotnet watch run"使用.Net Core 3,Visual Studio 2019和Docker

编程入门 行业动态 更新时间:2024-10-21 15:31:16
本文介绍了如何使用"dotnet watch run"使用.Net Core 3,Visual Studio 2019和Docker的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用Visual Studio 2019使用Docker和.NET Core3.我通过将Dockerfile添加到我的项目中来对我的应用进行容器化(右键单击该项目->添加-> Docker支持),并且能够启动它,但是现在我想在容器内使用 dotnet watch run .

I'm playing with docker and .NET Core 3 using Visual Studio 2019. I containerize my application by adding the Dockerfile to my project (right click on the project -> Add -> Docker Support) and I was able to launch it, but now I want to use dotnet watch run inside the container.

这是生成的Dockerfile:

This is the generated Dockerfile:

FROM mcr.microsoft/dotnet/core/aspnet:3.0-buster-slim AS base WORKDIR /app EXPOSE 80 EXPOSE 443 FROM mcr.microsoft/dotnet/core/sdk:3.0-buster AS build WORKDIR /src COPY ["DockerTestApp/DockerTestApp.csproj", "DockerTestApp/"] RUN dotnet restore "DockerTestApp/DockerTestApp.csproj" COPY . . WORKDIR "/src/DockerTestApp" RUN dotnet build "DockerTestApp.csproj" -c Release -o /app/build FROM build AS publish RUN dotnet publish "DockerTestApp.csproj" -c Release -o /app/publish FROM base AS final WORKDIR /app COPY --from=publish /app/publish . ENTRYPOINT ["dotnet", "DockerTestApp.dll"]

我这样修改了它:

FROM mcr.microsoft/dotnet/core/sdk:3.0-buster AS build ENV DOTNET_USE_POLLING_FILE_WATCHER 1 WORKDIR /src EXPOSE 80 EXPOSE 443 COPY ["DockerTestApp/DockerTestApp.csproj", "DockerTestApp/"] RUN dotnet restore "DockerTestApp/DockerTestApp.csproj" ENTRYPOINT ["dotnet", "watch", "run"]

以 dotnet watch run 开头的容器,但未检测到任何文件更改,也不会触发重建.

The container started with dotnet watch run but any file change isn't detected and the rebuild is not triggered.

我是否必须将代码目录中的卷挂载到容器上才能正常工作?

Should I have to mount a volume from my code directory to the container in order to make it work?

谢谢.

更新

使用此Dokerfile

with this Dokerfile

FROM mcr.microsoft/dotnet/core/sdk:3.0 ENV DOTNET_USE_POLLING_FILE_WATCHER 1 WORKDIR /app COPY . . ENTRYPOINT dotnet watch run --urls=+:5001 --project DocketTestApp.csproj

和这个docker-compose.yml

and this docker-compose.yml

version: '3.4' services: dotnet-watch-docker-example: container_name: dotnet_watch_docker_example image: giuseppeterrasi/dotnet-watch-docker-example build: context: ./DocketTestApp/ ports: - 5001:5001 volumes: - './DocketTestApp/:/app/' depends_on: - db db: image: mysql restart: always ports: - "3306:3306" environment: MYSQL_ROOT_PASSWORD: testPassword MYSQL_DATABASE: testDB MYSQL_USER: testUser MYSQL_PASSWORD: test

它可以工作,但是如果我添加了一个DbContext,则在容器启动时Visual Studio会错过Entity Framework引用.如果我停止容器并重新加载Visual Studio,一切正常.

It works, but if I add a DbContext, Visual Studio misses the Entity Framework reference if the container is started. If I stop the container and reload Visual Studio everything is ok.

为什么?

推荐答案

当您要在本地运行 dotnet watch run 时,可以省略使用自定义Dockerfile.

You can omit using a custom Dockerfile when you want to run dotnet watch run locally.

考虑以下 docker-compose.yml 文件:

version: '3.4' services: dotnet-watch-docker-example: container_name: dotnet_watch_docker_example image: mcr.microsoft/dotnet/core/sdk:3.0 ports: - 5001:5001 volumes: - ./DockerTestApp:/app working_dir: /app command: dotnet watch run

撰写文件并没有从基础dotnet sdk图像创建自定义图像,而是仅基于基础dotnet sdk图像启动了一个容器.然后,它创建一个卷,该卷将包含您的项目的本地目录映射到容器内的目录/app.然后将容器内的工作目录设置为/app,最后,在容器内运行dotnet watch run命令.

Instead of creating a custom image from the base dotnet sdk image, the compose file simply starts a container based on the base dotnet sdk image. It then creates a volume that maps the local directory containing your project to the directory /app inside the container. It then sets the working directory inside the container to /app, and lastly, it runs the dotnet watch run command inside the container.

要解决有关实体框架参考的问题,请在项目目录中添加以下 Directory.Build.props 文件.该文件指示MSBUILD根据执行环境将/bin和/obj文件放置在不同的目录(容器/本地)中.这样,就不会出现冲突.

To fix your problem with the Entity framework reference, add the following Directory.Build.props file inside the project directory. This file instructs MSBUILD to place /bin and /obj files in different directories (container/local) dependent upon the executing environment. This way, no conflicts emerge.

<Project> <PropertyGroup> <DefaultItemExcludes>$(DefaultItemExcludes);$(MSBuildProjectDirectory)/obj/**/*</DefaultItemExcludes> <DefaultItemExcludes>$(DefaultItemExcludes);$(MSBuildProjectDirectory)/bin/**/*</DefaultItemExcludes> </PropertyGroup> <PropertyGroup Condition="'$(DOTNET_RUNNING_IN_CONTAINER)' == 'true'"> <BaseIntermediateOutputPath>$(MSBuildProjectDirectory)/obj/container/</BaseIntermediateOutputPath> <BaseOutputPath>$(MSBuildProjectDirectory)/bin/container/</BaseOutputPath> </PropertyGroup> <PropertyGroup Condition="'$(DOTNET_RUNNING_IN_CONTAINER)' != 'true'"> <BaseIntermediateOutputPath>$(MSBuildProjectDirectory)/obj/local/</BaseIntermediateOutputPath> <BaseOutputPath>$(MSBuildProjectDirectory)/bin/local/</BaseOutputPath> </PropertyGroup> </Project>

更多推荐

如何使用"dotnet watch run"使用.Net Core 3,Visual Studio 2019和Docker

本文发布于:2023-11-17 04:51:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1608896.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   watch   run   dotnet   Net

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!