I had issues in my .NET projects while using hot-reload functionality and Docker.

Everytime I would update the code, all the references on my host machine would get broken and everything inside my IDE would get red lines.

I’ve realized this was due to how I set up the volumes - I mounted all the files, and everytime my app was rebuilt inside the container, it would overwrite the paths with the container paths.

Solution was to mount only the files that I needed - exclude build files.

There are probably better solutions, but for my local development this worked, so I am saving it for future reference.

Example API service from docker-compose.yml:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
services:
  
  api:
    build:
      context: ../../                
      dockerfile: environments/dev/Dockerfile
    ports:
      - "5000:5000"
    environment:
      - ConnectionStrings__DefaultConnection=User ID=${POSTGRES_USER};Password=${POSTGRES_PASSWORD};Server=${POSTGRES_SERVER};Port=${POSTGRES_PORT};Database=${POSTGRES_DB};
      - ASPNETCORE_URLS=http://+:5000
      - ASPNETCORE_ENVIRONMENT=Development
      - DOTNET_USE_POLLING_FILE_WATCHER=1
    volumes:
    # Avoid IDE reference issues for hot reload by separating volumes
    # Mount each source folder separately
    - ../../API:/src/API
    - ../../Application:/src/Application
    - ../../Domain:/src/Domain
    - ../../Persistence:/src/Persistence
    # Isolate bin/obj for each project
    - /src/API/bin
    - /src/API/obj
    - /src/Application/bin
    - /src/Application/obj
    - /src/Domain/bin
    - /src/Domain/obj
    - /src/Persistence/bin
    - /src/Persistence/obj
    working_dir: /src/API
    env_file:
      - ../../.env
    depends_on:
      - db
    restart: always
    command: dotnet watch --no-hot-reload --project API.csproj run --urls=http://0.0.0.0:5000