Developing with Docker and ASP.NET Core


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

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

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.



Shawn
Shawn Wildermuth
Author, Teacher, and Coach




My Courses

Wilder Minds Training
Vue.js by Example
Bootstrap 4 by Example
Intro to Font Awesome 5 (Free Course)
Pluralsight
Designing RESTful Web APIs (new)
Building an API with ASP.NET Web API
Building an API with ASP.NET Core
Building a Web App with ASP.NET Core, MVC6, EF Core, Bootstrap and Angular
Less: Getting Started

Application Name WilderBlog Environment Name Development
Application Ver v4.0.30319 Runtime Framework x86
App Path D:\home\site\wwwroot\ Runtime Version .NET Core 3.0.0
Operating System Microsoft Windows 10.0.14393 Runtime Arch X86