我正在使用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
发布评论