Containerize a .NET Core Web Api App with Docker

Rimaz Mohommed
5 min readFeb 8, 2024

In this article I will be showing you how easy it is to containerize your .net core 8.0 web api app using Docker. Although I’m specifically going to show you how to create containers for your .net core app, you can use the same approach to containerize apps in most other programming languages.

Prerequisites

Creating your .NET Core Web API application

If you already have a pre-existing .net core application in your machine, you can skip this part. Otherwise follow on!

For our purposes we will be creating a .net core api using the Clean.Lean.Architecture.WebApi.AspNetCore solution template : https://www.nuget.org/packages/Clean.Lean.Architecture.WebApi.AspNetCore.Solution.Template/

In your teminal run the following command to install the solution template

dotnet new install Clean.Lean.Architecture.WebApi.AspNetCore.Solution.Template

Next Once installed, create a new solution using the template. We will be naming our solution CleanLeanArchSln

dotnet new cla-sln --name CleanLeanArchSln

Launch the app:

cd .\CleanLeanArchSln\src\WebApi\

dotnet run

You can now navigate to the url denoted by the dotnet run output, in our case https://localhost:7292 to access the WebApi. Add the /swagger suffix to the URL to view the swagger file for the API.

We’ve now got a proper dotnet core web api app setup!

Containerize your .NET Core Web API application

Containerizing your .net core web api app can be quickly done by running thedocker init command at the root of your api solution.

Once you run docker init, you will be asked a number of questions regarding your application platform and version of dotnet among other things as you can see from the screenshot above.

Choose ASP.NET Core as your application platform.

In our case select the WebApi project which is our startup project in our solution.

For .NET version select 8.0

I’ve also set the port to be 8080.

Once docker init completes, it will create you 4 files

  • .dockerignore — this file will contain patterns to files and dirs that should be ignored when creating your docker container or image.
  • Dockerfile — this contains the steps to build your docker container.
  • compose.yml- contains the services that will be run.
  • README.Docker.md- a readme file for your containerization details.

Lets now build a docker image by using this generated Dockerfile from the root of our solution. You can also specify tags for you image using the -t flag. I will be specifying two tags- latest & 8.0.0 to reflect the .net core version of my web api app. As I will be pushing this image to my docker hub repository after creating the image, I will be using my account identifier rimaz523 as a prefix to the image name, followed by a colon : and then tag name.

NOTE : as the default tag is latest, you don’t need to specify it.

docker image build -t rimaz523/clean-learn-architecture-solution -t rimaz523/clean-learn-architecture-solution:8.0.0 .

Also note the period . at the end of the command. This lets docker know to create the image from the current directory that contains our Dockerfile.

Once our docker container image has been successfully created you can now view it by running docker image ls

Note that we have 2 images. One with the 8.0.0 tag and another with the latest tag which will be the default download if someone tries to pull our image from a container registry without specifying a tag. Underneath thouth its just one image with 2 tags!

Optionally lets now push our newly created docker image to docker hub. We will be pushing images with both the latest and 8.0.0 tags.

docker image push rimaz523/clean-learn-architecture-solution

docker image push rimaz523/clean-learn-architecture-solution:8.0.0

You should now be able to see your container image in docker hub repositories along with both tags.

Lets now experiment with removing our local image and then pulling down the latest image for our container from docker hub.

To remove our local images run the following command

docker image rm rimaz523/clean-learn-architecture-solution

docker image rm rimaz523/clean-learn-architecture-solution:8.0.0

Next run docker image ls to confirm that your image was deleted

Now lets pull down the latest image for our containerized dotnet core application from docker hub

docker pull rimaz523/clean-learn-architecture-solution

Now it’s finally time to run our containerized dotnet core app. We will map our machine’s port 8000 to the containers port 8080 (which we specified as the port the app will listen to in the container in our Dockerfile).

To run in detached mode, use the -d flag. (for interactive mode use -it flag instead of -d)

docker run -d -p 8000:8080 rimaz523/clean-learn-architecture-solution

You can then optionally run docker ps if you want to view the running containers.

To access the swagger file of the dotnet core app running in the container, open your browser and go to port 8000 which was defined as your mapped machine port: http://localhost:8000/swagger/index.html

Nice work!! You now have a containerized .NET Core app running in a docker container!

--

--