In this part, we are going to apply some modifications to our ASP.NET Core application in order to prepare it for the dockerization process.

To follow along with this part you need .NET Core 2.0, Git, an IDE of your choice, and Docker.

For the purposes of this article, we are going to re-use the application from our .NET Core Series, more concretely part 6 of the series. The main reason for this decision is that if you are not familiar with the ASP.NET Core, you can go through the series and learn how to make ASP.NET Core WebApi application from scratch.

Since we are going to make some changes to the code, if you want to follow along with the steps of this course, you can find the starting point on the master branch docker-series

If you want to skip the preparation phase and jump right into Docker goodness, you can switch to the docker-series-app-prepared branch of the docker-series-app-prepared repo and go quickly through this section. It might be beneficial for you to follow along if you are not familiar with some of the changes we are going to implement.

This is the first article of our Docker Series. You can find the other parts on our Docker Series page.

These are the changes we’re going to make to the project:

There is a lot to cover, so let’s get down to business.

Switching to an In-Memory Database

First things first.

To be able to dockerize our app and make it work without relying on external resources (database) we are going to make some changes to the configuration. We are going to re-introduce MySQL later in the series, but for now, we are going to rely on an in-memory database to store our data into.

So, let’s modify our ServiceExtensions.cs class:

That’s it!

We successfully removed the reference to the MySQL database and added an in-memory database in a single line of code.

Isn’t that just great?

Don’t worry about the hanging connection string in the appsettings.json file, we don’t need to remove it since we are going to use it in the next part.

Introducing AccountController

To make our API functional on its own, we are going to modify and rename existing ValuesController. So, first, we are going to rename it to AccountController, and then add some new code to it. We are going to add GetAllAccounts method and CreateAccount method which will give us more freedom to play with our API.

Of course, the compiler won’t let us build this since we need to implement GetAllAccounts and CreateAccount methods in our AccountRepository.

So let’s do exactly that.

Add the method declarations to the IAccountRepository:

And the implementation the AccountRepository:

Now run the dotnet build on the entire solution, and the project should compile.

And just like that, we have a working AccountController.

Introducing Swagger

Swagger is one of the most popular and versatile frameworks for tooling your APIs.

Here is how we are going to add it to our project.

Navigate to the /AccountOwner folder. After that execute the command:

This will add the reference to Swashbuckle package for ASP.NET Core to our project (2.3.0 is the current version, for now). In short, Swashbuckle will help us integrate Swagger into our application.

After that, we have to enable Swagger by changing our Startup.cs file a bit.

To the ConfigureServices method, just after the service.AddMvc() line, we are going to add:

And to the Configure method just after the app.UseMvc() line:

Great. That wasn’t too hard, was it?

But, what have we gained by doing all this?

If you run the application now and go navigate to http://localhost:5000/swagger/v1/swagger.json to see the information about your API presented in JSON format.

This information is the power behind the more interesting part of Swagger, the interactive documentation which you can get by navigating to http://localhost:5000/swagger. If you did everything right you should see the documentation page like this one:

Swagger UI

This is a great way to document and explore an API. Since our application has no frontend attached, you can test the API directly using this documentation page, without the need to use external tools like Postman or Fiddler.

Let’s move on.

Modifying the launchSettings.json File

We are going to change the http://localhost:5000/ to http://[::]:5000/ because otherwise, we are going to have problems with exposing our application to the local environment. works too.

Adding the Test Project and a Unit Test

For the testing purposes, we’re going to use xUnit tool and Moq for mocking our data.

The easiest way to add a testing project in ASP.NET Core is with the dotnet CLI. Navigate to the root of the solution and type:

This will create the project using the xUnit template and restore the needed packages.

After that we can add the project to the solution:

And because we need to use Moq library, we are going to navigate to Tests project and add it too:

And finally, reference the projects we need in order to write unit tests:

That’s it for the setup part.

Now we can easily add our first unit test to the project.

First, rename the default UnitTests1.cs file to something more appropriate like OwnerRepositoryTests.cs in which we can test our owner repository logic.

Now, let’s add our unit test:

This is a simple demo test to check if the GetAllOwners method returns a list of Owner objects and if it returns exactly one owner like we defined in GetOwners helper method.

Now if you run dotnet test you should get something like this:

Test Results


We have completed the preparation part of the project. We have a working ASP.NET Core application, together with interactive documentation and some unit tests.

Everything is set for the next step, which is why we did all this preparation for and that’s the Dockerization of our ASP.NET Core application.

To check if everything works alright, run dotnet build and dotnet test. This is how we do it usually during the development phase. But, to lift this up a level, we’re going to try it out by using Docker images and containers in this series.


That wraps it up for this part of the series. Although we haven’t touched Docker yet, the changes we made here are important for the parts to come. These changes will help us soften the learning curve by introducing the concepts one by one.

In the next part, we are going to learn more about Docker, the reasons for using it, and we’re going to execute some basic Docker CLI commands.

You can find all the changes up to this point by checking out the docker-series-app-prepared branch of the docker-series repository.

If you have enjoyed reading this article, please leave a comment in the comments section below, and if you want to receive the notifications about the freshly published content we encourage you to subscribe to our blog.