.NET 6 docker container with watch

Viewed 245

How do I create a docker container that will run .NET with hot reload via dotnet watch run? Most of the solutions online go the right way but there is always something wrong like:

  • container starts fine but the port is not the one I specify and port forwarding directives dont work
  • container cannot be built because of "watch" is not something dotnet can run
  • project files cannot be found or copied and many others that I forgot to mention but encountered on my way to the solution
1 Answers

First of all run dotnet new webapi -o YOURPROJECTNAME to setup the development environment with a mock api controller.

Then place this docker-compose.yml file in your root:

version: "3.4"

services:
  app:
    image: YOURIMAGENAME
    build:
      context: .
      dockerfile: ./Dockerfile
    ports:
      - 8080:8080
    volumes:
      - .:/app
    depends_on:
      - db

Then add this Dockerfile:

FROM mcr.microsoft.com/dotnet/sdk:6.0-focal

WORKDIR /app
COPY . /app
RUN dotnet restore

ENTRYPOINT [ "dotnet", "watch", "run", "--no-restore", "--urls", "http://*:8080" ]

Then add this .dockerignore:

bin
obj

After all this is done run docker-compose up to run the container and voila, have fun developing APIs with .NET in a container with hot reload!

Related