Note: This post is based on ASP.NET 5 Beta8, the samples might not work for you anymore
Docker has becoming a popular way of hosting Linux based applications the last few years. With Windows Server 2016 Docker containers are also coming to Windows and are likely going to be a popular way of hosting there as well. But as Windows support is just in CPT form right now and ASP.NET is also moving to Linux with the CoreCLR I decided to try and run an ASP.NET web site on a Linux based container.
Getting started with an ASP.NET 5 application
To get started I installed the ASP.NET 5 beta 8 using these steps. Once these where installed I created a new ASP.NET MVC project. Just to show where the application is running I added a bit of code to the About page. When running from Visual Studio 2015 using IIS Express this looks like this:
The extra code in the HomeController:
1: public class HomeController : Controller
2: {
3: private readonly IRuntimeEnvironment _runtimeEnvironment;
4:
5: public HomeController(IRuntimeEnvironment runtimeEnvironment)
6: {
7: _runtimeEnvironment = runtimeEnvironment;
8: }
9:
10: public IActionResult About()
11: {
12: ViewData["Message"] = "Your application is running on:";
13: ViewData["OperatingSystem"] = _runtimeEnvironment.OperatingSystem;
14: ViewData["OperatingSystemVersion"] = _runtimeEnvironment.OperatingSystemVersion;
15: ViewData["RuntimeType"] = _runtimeEnvironment.RuntimeType;
16: ViewData["RuntimeArchitecture"] = _runtimeEnvironment.RuntimeArchitecture;
17: ViewData["RuntimeVersion"] = _runtimeEnvironment.RuntimeVersion;
18:
19: return View();
20: }
21: }
With the following Razor view:
1: @{
2: ViewData["Title"] = "About";
3: }
4: <h2>@ViewData["Title"].</h2>
5: <h3>@ViewData["Message"]</h3>
6:
7: <h4>OperatingSystem: @ViewData["OperatingSystem"]</h4>
8: <h4>OperatingSystemVersion: @ViewData["OperatingSystemVersion"]</h4>
9: <h4>RuntimeType: @ViewData["RuntimeType"]</h4>
10: <h4>RuntimeArchitecture: @ViewData["RuntimeArchitecture"]</h4>
11: <h4>RuntimeVersion: @ViewData["RuntimeVersion"]</h4>
12:
13: <p>Use this area to provide additional information.</p>
So far so good and we can see the application runs just fine on Windows using the full CLR. From Visual Studio we can also run the application using Kestrel with the CoreCLR as shown below.
Running the application in a Docker container
Having installed the Docker-Machine we can also run this same website in a Docker container on Linux. The first thing we need to do is create a Dockerfile with the following contents:
1: FROM microsoft/aspnet:1.0.0-beta8-coreclr
2:
3: COPY ./src/WebApplication3 ./app
4:
5: WORKDIR ./app
6: RUN dnu restore
7:
8: ENTRYPOINT dnx web --server.urls http://*:80
With this container definition in place we need to build the Docker container itself using the docker build -t web-application-3 . command. This is executed from the Docker terminal window in the main folder of our web application where the Dockerfile is located. This will build our Docker image which we can now see see when running docker images.
With this new image we can run it using: docker run -d -p 8080:80 web-application-3
With the application running we can navigate to http://192.168.99.100:8080/Home/About where 192.168.99.100 is the IP address of the virtual machine running the Linux machine with the Docker daemon.
Sweet