Weekend Project: Introduction to ASP.NET Core + Docker Applications

Gonzalo Alcaraz
The Startup
Published in
11 min readJun 24, 2019

As part of the selection process, a potential employer challenged me to learn about ASP.NET and Docker, make a simple application, and document the whole process. The timeframe given to me: one week. Since my schedule is pretty packed though (between 2 jobs and studying engineering), this meant I had to tackle the challenge as a weekend project. Hopefully, the reader (you!) will find this post useful as a guide to familiarize yourself with ASP.NET and Docker, develop something simple enough, and then move on to more complex stuff by yourself.

So… what is ASP.NET Core?

According to Microsoft’s .NET page, .NET is “…a developer platform made up of tools, programming languages, and libraries for building many different types of applications”. So it’s a software framework.

It also says that the ASP part “…extends the .NET platform with tools and libraries specifically for building web apps”.

Finally, Core is “…the open-source version of ASP.NET, that runs on macOS, Linux, and Windows”. So it’s cross-platform. Pretty neat!

There is a lot that’s been written on the “why” of ASP.NET Core, so I’ll not get much into it here. It’s free, open source, has Microsoft’s full support, a nice and big community, etc. Also, though the numbers may differ a bit, several web statistics pages agree that it’s the second most used framework across the entire internet (second only to PHP).

What convinced me about its value, however, are the results from Stack Overflow’s yearly survey: .NET Core stands at the top of the list of most loved frameworks (as of 2019). If anything should convince you to try it out, it’s this. Always listen to your peers (especially if they have more experience than you!).

Now that we have some idea of what it is we’re doing, let’s find out about the other half of the assignment…

The “what” and “why” of Docker

Have you ever tried bundling some code or app to run on another computer, only to find out that something’s missing and it just won’t work? Perhaps it’s something as simple as a .dll file, or it needs to install a particular framework. Sometimes your app is just not compatible with this new platform. Wouldn’t life be so much simpler if you could just forget about all this and just focus on creating great apps? In comes the concept of “containers”.

Containers are essentially small bundles where you can place your app with all its dependencies (libraries, configuration files, etc). It’s kind of like a virtual machine, but smaller in size and scope, and a single OS kernel can run several containers at once.

Docker works as a link between the OS and containers, contrary to a VM.

Docker does all of this for you, creating the container for your app and letting it run inside the Docker Engine. So all you need to run any of your apps anywhere is to have the container, and a version of the Docker Engine installed (which is highly cross-platform). Pretty cool, huh?

So now we know more or less what it is we’re doing. It seems to me there’s just one thing we need to determine before we get started with the project… where are we going to carry it out? Or rather, which IDE are we gonna be using?

Luckily, the choice for this comes pretty naturally: Visual Studio. There are several reasons for this. First, not only is it a personal preference of mine, but it was also chosen as the 2nd most liked IDE by the previously mentioned Stack Overflow survey (the 1st most liked was Visual Studio Code). Second, since it’s developed by Microsoft, it is completely integrated with ASP.NET Core. It’s also completely integrated with Docker. I can keep going on why it’s such a great choice, but these reasons should be more than enough for anyone.

Step by step guide

1 — Installing Visual Studio and ASP.NET Core Framework

Installing Visual Studio is a breeze nowadays (what used to take maybe hours now takes a couple of minutes). Just go to Visual Studio’s homepage and grab the latest community version of the IDE, which is completely free. It’s a feafeature-packedrsion, and I’ve never found any limitations to it on my personal projects.

Once the download completes and you run the installer, you’ll be greeted by the following screen:

You can pick as many options as you want (some of which are pretty interesting and I’ll be sure to check out in the future), but for this tutorial, all we need is the “ASP.NET and web development” option.

Once you pick the ASP.NET and web development option, you can start the installation. It took up almost 8 Gigs of space on my machine, but the download is much smaller (a bit less of 2 Gigs I believe).

2 — Installing Docker Desktop

Just go to the Docker Desktop page and click on the download link.

Installation is pretty straight-forward. Since in this example we’re developing an app for Windows, I checked the “Use Windows containers instead of Linux containers” option (don’t worry if you didn’t, as this can later be changed).

After the install program has finished, you will get a prompt asking you to enable Hyper-V virtualization, which will restart your computer. Just click “Ok” and wait for the restart.

Just click “Ok”

Once Windows boots up, you get a message saying “Docker is starting…” and… if your setup is anything like mine, the first problem (so far):

I guess I never needed virtualization before…

For some reason, “Hardware-assisted virtualization” was never enabled in my motherboard’s BIOS, so it’s time to enable it. Since BIOSs differ widely across the board (pun intended), it wouldn’t make much sense for me to show you exactly how I enabled the option in mine. Rather, I’ll point you to some simple tutorials on the matter:

In a nutshell, the whole process consists of restarting the computer, entering BIOS through a particular keystroke, locating the virtualization option (usually found in Advanced->CPU options, or something like it), and enabling it.

Now back to our regularly scheduled programming! Once that is done and we start Windows again, we’ll get the following message from Docker:

I guess we should take heed of what Docker says and start typing Docker commands!

That’s it! It works! But before we start putting it all together, maybe we should try a few things to familiarize ourselves with Docker first. It’s a good idea to follow the official Docker for Windows tutorial. Here are some things you can try out if you’re in a hurry:

The first thing you should do is open a command prompt (not PowerShell!) and run the docker --version command, which should return the currently installed version if the installation went well. And just like that, you can start downloading Images and running them on the console! Run docker run --interactive --tty ubuntu bashto download an Ubuntu image and run it locally as a container!

You can even run Linux commands on it, install packages, etc.

3 — Running some ASP.NET Core examples on Docker

So now that we’ve installed everything, let’s start putting it all together and having some fun (finally!).

The first thing we’ll do is downloading some .NET containers and running them with Docker. We’ll be following this tutorial (which I’ll replicate here to some extent), so if you need a bit more hand-holding be sure to check it out (Docker’s documentation is pretty great all around).

Let’s start by running git clone https://github.com/dotnet/dotnet-docker on a command line. This will clone Docker’s ASP.NET Core Examples repository.

Next, navigate to the docker file folder at dotnet-docker/samples/aspnetapp/aspnetapp. Now run thedotnet publish -c Release -o published command to build the application in release mode, and run it by executing dotnet published\aspnetapp.dll

Now just use any browser to go to the https://localhost:5000/ address, and there it is: the example page running on a container!

Success looks like this

There are other ways of running the application (what I showed here is just what I found easier), and you can follow the previously linked tutorial to try them out if you’re curious.

4 — Creating an example application

Before we get started, the code for the example app, as well as the dockerfile used, can be found on my Github/TechChallenge page, so you can check it out at your leisure.

I’ll be creating a web application using Razor Pages, which is supposed to be a new and easier way to code page-focused scenarios. After trying them out, I can say it is quite simple and fun!

Your project creation screen should have these options selected when you create it.

Be sure to have the “Enable Docker Support” option ticked, as it will make publishing the image a breeze.

I’d like to call your attention to a couple of things on the project structure, so let’s take a couple of minutes to talk briefly about it.

You’ll see that, since we’ve enabled Docker support, there’s a Dockerfile at the project root. This type of file is the recipe for creating a final Docker image. For a more advanced approach, you can read about some of the best practices for writing these files here.

These are the project files as seen on the Solution Explorer. Let’s talk just a bit about some of them.

The Pages folder holds Razor pages (duh!) and supporting files. Each page consists of a .cshtml file that contains the HTML format of the page, and a .cs file that contains the C# code that handles events on the page. So, if you’re not very familiar with this, basically the .cshtml file configures how the page looks, and the .cs file determines the page’s behavior, or what it does. For example, in the application I’m using as an example, I set up a “Time” property in the .cs file, which returns the current time. On the .cshtml file, I invoke said property to render the current time on-screen whenever the page is opened.

Declaration of the “Time” property on the .cs file.
Invocation of the “Time” property on the .cshtml file.

Two very important files are Program.cs and Startup.cs. The first one is the entry point for the program or the equivalent of what in other programming languages or types of applications is the “main”. It also calls the Startup.cs file, which contains code that configures app behavior and loads needed services and configuration the app is going to need to run.

There are lots of things you can do quite easily with Razor pages, and I invite you to explore the vast amount of tutorials on the matter (this one might be a good start). Setting up databases with CRUD functionality and associated pages is a pleasure really, though it might require some work to integrate it with Docker (you might start reading some of these posts and projects to get started on that!).

So, now that we’ve got a (very) rudimentary app up and running, it’s time to dockerize it!

5 — Dockerizing the App

So, if you’ve been following this guide, you should be running the app on Visual Studio with Docker support enabled, and you should have been able to run and open the app on your browser just by pressing Ctrl+F5 on Visual Studio.

This leaves the container running on Docker, and VS will update it every time you run the app again. You can check it’s running even after you close the page by using the docker ps command.

The container running from the VS Run command (top), and the container running from the manual build and run command (which we’ll see about in a minute…).

Another way to do build the container and run the app is to use the Dockerfile. I’ve mentioned it before, but I’ll get into a bit more detail here.

This is the dockerfile that comes pre-generated by VS Docker support.

What matters here is the “ENTRYPOINT” part, which, as you can see, targets the project .dll, so the name has to be right (it will be if you’re using the generated file). You can leave everything as is but note also the “FROM” command, as this tells Docker to pull an image (in this case, from ASP.NET Core) to build the rest of the container with. Docker Hub has a long list of images for all your project needs, so be sure to dive into it to see what else you can do with it.

So, we have a Dockerfile ready to build the container, but I’ve run into a bit of a problem, or rather, a bug. It seems Visual Studio generates the file in the wrong place, and when you try to run it, there’s going to be errors about halfway through. No matter though, as some forum posts later and I found all you need to do is move the file to the solution root folder (rather than the project folder) and run it from there with the following command:

docker build -t techchallenge .

The -t is followed by a tag, so really you can just write whatever there. The console output should look something like this:

Now just run the “run” command (you can select other ports if you want):

docker run -d -p 8080:80 --name techchallenge techchallenge

And you have the container running, again. This time, you can access it by writing http://localhost:8080/ on any browser. The “docker images” command will list it as one of your local images too:

I hope you’ve found this post helpful, and please feel free to leave your feedback or questions on the comment section or to my email g.alcaraz@outlook.com. Have a good one!

Appendix: Next steps

Something I’d love to continue working on but did not find the time yet is making docker work with databases. I followed this quite simple and fun tutorial (you can get the code itself here) on using Razor pages with local databases, only to find out that VS Docker support doesn’t bundle them inside the container; at least, not trivially. It seems the simplest way is to make a SQL image container, and link your application container to that one using Docker composer or something equivalent (yes, naturally, everything gets more complex once you dive into it enough). I’d probably start with this tutorial on running a containerized API with SQL.

What I’d love to do, however, is to plunge deep into what ASP.NET Core has to offer. It seems like a really powerful tool, one quite capable of delivering really exciting and complex applications relatively quickly. Microsoft has a cool learning platform, but there are so many tutorials on the web that you can be sure to find something that piques your interest anywhere.

About me

I’m about to graduate from electronic engineering and have worked for a few years researching RF applications and designing hardware and embedded systems. I love keeping up on tech, listening to podcasts, and am trying to incorporate new thinking models all the time. I try to keep my mind challenged and busy always, but also my body: I’m an avid runner (doing mostly trail races), and a newbie orienteer and mountaineer. What about you?

--

--

Gonzalo Alcaraz
The Startup

Software Engineer. Climber. Mountaineer. Runner. Formerly, hardware & embedded systems designer.