How to use "dotnet watch run" with .Net Core 3, Visual Studio 2019 and docker How to use "dotnet watch run" with .Net Core 3, Visual Studio 2019 and docker docker docker

How to use "dotnet watch run" with .Net Core 3, Visual Studio 2019 and docker


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

Consider the following docker-compose.yml file:

version: '3.4'services:  dotnet-watch-docker-example:    container_name: dotnet_watch_docker_example    image: mcr.microsoft.com/dotnet/core/sdk:3.0    ports:      - 5001:5001    volumes:      - ./DockerTestApp:/app    working_dir: /app    command: 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.

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>


Change the entry point like this,

ENTRYPOINT dotnet watch run --no-restore

This will rebuild the server whenever new changes occurs.