Hosting an ASP.NET 5 site in a Linux based Docker container

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:

image

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.

image

 

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.

image

With this new image we can run it using: docker run -d -p 8080:80 web-application-3

image

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.

image

Sweet Smile

2 thoughts on “Hosting an ASP.NET 5 site in a Linux based Docker container

  1. no luck running machos 10.10 docker this are my logs
    bash-3.2$ docker logs 450db12f309b
    Could not open /etc/lsb_release. OS version will default to the empty string.
    Application startup exception: System.IO.FileLoadException: Could not load file or assembly ‘app, Culture=neutral, PublicKeyToken=null’ or one of its dependencies. General Exception (Exception from HRESULT: 0x80131500)
    File name: ‘app, Culture=neutral, PublicKeyToken=null’ —> Microsoft.Dnx.Compilation.CSharp.RoslynCompilationException: /app/Controllers/HomeController.cs(11,20): DNXCore,Version=v5.0 error CS1002: ; expected
    /app/Controllers/HomeController.cs(13,43): DNXCore,Version=v5.0 error CS1001: Identifier expected
    /app/Controllers/HomeController.cs(13,63): DNXCore,Version=v5.0 error CS1001: Identifier expected
    /app/Controllers/HomeController.cs(11,10): DNXCore,Version=v5.0 error CS0246: The type or namespace name ‘read’ could not be found (are you missing a using directive or an assembly reference?)
    /app/Controllers/HomeController.cs(11,20): DNXCore,Version=v5.0 error CS0246: The type or namespace name ‘IRuntimeEnvironment’ could not be found (are you missing a using directive or an assembly reference?)
    /app/Controllers/HomeController.cs(13,24): DNXCore,Version=v5.0 error CS0246: The type or namespace name ‘IRuntimeEnvironment’ could not be found (are you missing a using directive or an assembly reference?)
    /app/Controllers/HomeController.cs(13,45): DNXCore,Version=v5.0 error CS0246: The type or namespace name ‘runtimeEnvironment’ could not be found (are you missing a using directive or an assembly reference?)
    /app/Controllers/HomeController.cs(15,27): DNXCore,Version=v5.0 error CS0103: The name ‘runtimeEnvironment’ does not exist in the current context
    /app/Controllers/HomeController.cs(11,15): DNXCore,Version=v5.0 warning CS0169: The field ‘HomeController.only’ is never used
    at Microsoft.Dnx.Compilation.CSharp.RoslynProjectReference.Load(AssemblyName assemblyName, IAssemblyLoadContext loadContext)
    at Microsoft.Dnx.Compilation.CompilationEngine.LoadProject(Project project, FrameworkName targetFramework, String aspect, IAssemblyLoadContext loadContext, AssemblyName assemblyName)
    at Microsoft.Dnx.Runtime.Loader.ProjectAssemblyLoader.Load(AssemblyName assemblyName, IAssemblyLoadContext loadContext)
    at Microsoft.Dnx.Runtime.Loader.ProjectAssemblyLoader.Load(AssemblyName assemblyName)
    at Microsoft.Dnx.Host.LoaderContainer.Load(AssemblyName assemblyName)
    at Microsoft.Dnx.Host.DefaultLoadContext.LoadAssembly(AssemblyName assemblyName)
    at Microsoft.Dnx.Runtime.Loader.AssemblyLoaderCache.GetOrAdd(AssemblyName name, Func`2 factory)
    at Microsoft.Dnx.Runtime.Loader.LoadContext.Load(AssemblyName assemblyName)
    at System.Runtime.Loader.AssemblyLoadContext.LoadFromAssemblyName(AssemblyName assemblyName)
    at System.Runtime.Loader.AssemblyLoadContext.Resolve(IntPtr gchManagedAssemblyLoadContext, AssemblyName assemblyName)
    at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
    at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
    at System.Reflection.Assembly.Load(AssemblyName assemblyRef)
    at Microsoft.AspNet.Hosting.Startup.StartupLoader.FindStartupType(String startupAssemblyName, IList`1 diagnosticMessages)
    at Microsoft.AspNet.Hosting.Internal.HostingEngine.EnsureStartup()
    at Microsoft.AspNet.Hosting.Internal.HostingEngine.EnsureApplicationServices()
    at Microsoft.AspNet.Hosting.Internal.HostingEngine.BuildApplication()
    Hosting environment: Production
    Now listening on: http://*:80
    Application started. Press Ctrl+C to shut down.
    bash-3.2$

    1. Eduardo,

      The stack trace suggest an error during compilation that IRuntimeEnvironment can’t be resolved. Did everything work just fine on your development machine?

Leave a Reply

Your email address will not be published. Required fields are marked *