Rants and Raves

Thanks for visiting my blog!

Developing with Docker and ASP.NET Core
Developing with Docker and ASP.NET Core
December 26, 2024

If you’ve viewed my new “Designing RESTful Web APIs” course on Pluralsight, you’ve already encountered my small API that I use as an exammple. While the course is platform agnostic, i’ll admit that I built it with ASP.NET Core (2.2).

For this new project, I decided that running in a container would be useful as it’s pretty self-contained and should just return to it’s initial state when the container is restarted. While I won’t be releasing that source, I did learn a lot about debugging in a container that I thought I’d share.

I started this new project and opted into using Docker in the project:

Enabling Docker on a new project

That short link for “Docker Desktop” is important to making this work. So make sure you have it installed by going to:

https://www.docker.com/products/docker-desktop

Installing this (mac or pc) will run docker and allow you to create and host docker containers. Visual Studio will use Docker Desktop to host your app inside of during development to allow you to develop or test in.

The trick of it is that you get a new item in your launchSettings.json:

{
    ...
    "Docker": {
      "commandName": "Docker",
      "launchBrowser": true,
      "launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}/api/values",
      "httpPort": 65032
    }
  }
}

This, in conjuction with a docker file in your project (just called “Dockerfile” without an extension) that builds a container, hosts it, and allows you to debug it remotely. For me this was important because I knew I wanted to host it in a linux container and being able to validate I didn’t have any code that wouldn’t work well on linux gave me peace of mind.

The Dockerfile is pretty simple (if you’re familiar with Docker) though it’s pretty much left alone so you don’t have to know how it works:

FROM mcr.microsoft.com/dotnet/core/aspnet:2.2-stretch-slim AS base
WORKDIR /app
EXPOSE 80

FROM mcr.microsoft.com/dotnet/core/sdk:2.2-stretch AS build
WORKDIR /src
COPY ["DesigningApis.csproj", ""]
RUN dotnet restore "DesigningApis.csproj"
COPY . .
WORKDIR "/src/"
RUN dotnet build "DesigningApis.csproj" -c Release -o /app

FROM build AS publish
RUN dotnet publish "DesigningApis.csproj" -c Release -o /app

FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "DesigningApis.dll"]

```csharp

It creates a container from a slim image with ASP.NET Core 2.2 (in this case), builds the project and then does a publish to get just a copy of the project in a container.  To test/debug with docker, just pick the 'Docker' option in the debugging dropdown:

![picking docker](https://wilderminds.blob.core.windows.net/img/picking-docker.png)

In my case, I'm running Docker Desktop using linux images (not windows images) but if you intend on deploying to Windows, you could switch that pretty easily. I'm pretty thankful Microsoft has made it this easy to test against containers.