dockerise the asp.net core example application.

Ivory Wolf
2 min readDec 30, 2020

--

New to Docker? Got it installed and want to run an asp.net core application from a container.

Assumes you have installed Docker Desktop.

  1. Use Visual Studio (or VS Code) to create the basic asp.net core web API project. You know, the one with the Weather Forecast controller example. Build and run, make sure it all works OK.
  2. Create a Release build of the above project.
  3. Open the project in VS Code and add two files: Dockerfile and .dockerignore (no file extensions needed)

4. In the Dockerfile file add:

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1COPY bin/Release/netcoreapp3.1/ App/WORKDIR /AppENTRYPOINT ["dotnet", "ContainerWeb.Api.dll"]

Note: The example project was named ContainerWeb.Api — adjust your text accordingly on the last line and in the subsequent commands.

The above code tells docker build to:
- Include the latest dotnet core 3.1 runtime in the image — so our code will run.
- Copy our compile project into a folder called ‘App’ within the image.
- Define the App folder in the image as the working folder (set the context for any commands)
- Define what to do when the container starts. In this case, the entry point is the command: dotnet ContainerWeb.Api.dll (defined as a json array of strings)

5. in the .dockerignore file add any files you want to ignore from your context (bin/Release/netcoreapp3.1/). This example makes sure we don't add .pdb (debugger information) into the release image.

**/*.pdb

5 Build the image. From a VS Code terminal:

docker build -t containerweb .
  • t enables us to ‘tag’ or name the image with “containerweb” use any name you like.
  • don't forget the period at the end — the location to build, which, in this and most cases is the location were are running in.

6. Run the image and create the container. From VS Code terminal:

docker run -d -p 8080:80 --name mycontainer containerweb
  • -d : runs detached, so the terminal will be free to run other commands
  • -p : maps port 8080 to port 80 that kestrel will be listening on in the container.
  • — name: allows us to name this instance mycontainer. Without this, a random name will be assigned.
  • containerweb : the image to run — the one created above or whatever you named your image.

6 Browse to HTTP://localhost:8080/weatherforecast (note not HTTPS)

--

--