dotnet watch crashes in docker?

Viewed 512

When I try to run dotnet watch through docker (on windows, wsl) I get an error like Unhandled exception. System.ArgumentException: An item with the same key has already been added. Key: /src/bin/Debug/net5.0/Tickets.runtimeconfig.json. The Key: ... is always different.

This error sometimes happens while building:

app-watch_1  | Building...
app-watch_1  | Unhandled exception. System.ArgumentException: An item with the same key has already been added. Key: /src/bin/Debug/net5.0/Tickets.runtimeconfig.json

But sometimes the build succeeds, but then the error occurs randomly at other times.

It seems to happen even in clean projects. I created a new project using (this is the part that still works):

dotnet new web

Then added a docker file:

FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build-env
WORKDIR /app

# Copy csproj and restore as distinct layers as an optimizations
COPY *.csproj ./
RUN dotnet restore

# Copy the rest
COPY . ./
RUN dotnet publish -c Release -o out

# Build runtime image without all the bagage from the build steps
FROM mcr.microsoft.com/dotnet/aspnet:5.0
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "Tickets.dll"]

Added a docker-compose.yml file:

version: '3.4'

services:
  tickets:
    build:
      context: .
    ports:
      - 8880:80

And run it with: docker-compose build and then docker-compose up. This worked fine.

Then I tried adding a service to my docker-compose file to use dotnet watch:

(full docker-file)

version: '3.4'

services:
  tickets:
    build:
      context: .
    ports:
      - 8880:80

  tickets-watch:
    image: mcr.microsoft.com/dotnet/sdk:5.0
    ports:
      - 8881:80
    volumes:
      - ./:/src
    working_dir: /src
    command: dotnet watch run --urls=http://+:80

When I then try docker-compose up again, I sometimes get the error. At other times the build succeeds. It seems that once the build succeeds once, it's fine. But when I quit docker, remove the bin and obj folders and then start docker again, there is again a chance it will fail.


Here are some of the things I tried (I'm sure I tried more stuff, but can't remember all of it, sorry about that.):

I thought it might have something to do with that I have VSCode open in the same folder, so I added a Directory.Build.props file:

<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>

That did not help.

I also removed docker desktop, and installed docker inside of WSL (following: https://dev.to/bowmanjd/install-docker-on-windows-wsl-without-docker-desktop-34m9), but that also didn't help.

I tried running the same project on my Linux pc, there it worked perfectly. So def seems to be something with windows/wsl.

Thanks!

2 Answers

I believe I solved the problem myself, by moving the code into my home folder on WSL, instead of having it on my c-drive (and then accessing it through /mnt/c/...).

So, when my code was in /mnt/c/Users/Pablo/Dev/Tickets I got the crashes.

I then put my code in ~/dev/Tickets and the crashes went away.

I managed to fix that error by adding

- /src/ProjectFolder/bin/
- /src/ProjectFolder/obj/

to volumes in docker-compose.yml, so it looked like

volumes:
  - ./:/src
  - /src/ProjectFolder/bin/
  - /src/ProjectFolder/obj/

I still don't exactly know why is this works, but it makes these folders in container separate from those on the host.

Related