Publish a Web App Into Docker Container From Visual Studio

As most of you may know already that Docker has released a beta version of their Windows avataar some time ago. Unlike the stable version, this one is using native components like Powershell and Hyper-V to leverage it's capabilities in the local machine. You can read more about the beta program and the installation procedure in the links given below.

Docker for Windows Beta announced
Installing Docker For Windows Beta
Native Docker comes to Windows and Mac

Installing Visual Studio Tools For Docker

The Visual Studio team also released some tooling for creating containers directly from VS as well as for debugging application hosted in Docker. These are called Visual Studio Tools for Docker and is available for download from the following link.

Download Visual Studio 2015 Tools for Docker

The installer is around 30 Megs in size and the installation process is pretty straightforward. You just need to double click on the downloaded installer to start the installation. The first screen will be as shown below, and to start the installation you will need to agree the license terms by clicking on the checkbox.

Creating a ASP.NET Core Project.

To see the tools in action let's create a ASP.NET Core Web application from File -> New Project dialog box. Select the ASP.NET Core Web Application option from the window, give a name for the project and then click on the OK button.

From the template window, choose Web Application and click the OK button for creating the project.

Adding Docker Support

Now we need to add support for Docker in our project. For that right click on the project to bring up the context menu, select Docker Support from the Add option for adding it to our project. 

You can verify everything is in place by checking the Properties folder as well as the Docker folder from the solution explorer. It will have all the resources needed for creating the container as well as for deploying it into Docker. Also the option for Docker will be available in the Debugging options and it will be set as the default one when we add the Docker support to our project.

Deploying to Docker

Let's start the build by pressing the F5 button or the play button from the IDE. Make sure that you have selected the docker option, otherwise the output will be rendered in the browser in the local machine. You can use the output window to see the verbose information about the various steps as shown in the image below. You will notice that VS is using the DockerTask.ps1 powershell script which got added when we enabled Docker Support in the project. It will

  1. Build and publishes the application.
  2. Creates a Docker image using it
  3. Then creates and runs a container using the newly created image
  4. Loads the web application in the default browser, the IP address corresponds to IP of your Docker host and uses port # 80 by default.
Building and Publishing

 

Starting the newly created container

Loading the site in the default browser

    

Home page loaded iin local browser

   

Viewing Images and Container from Powershell

You can use the docker ps and docker images commands in powershell prompt to view the newly created images and containers.

The below screenshot lists the container created for our web application, it shows relevant information such as image id, image name, when was it created, what's it's current status and which IP address is using etc.

 

The docker images command will list all the available images in Docker, in my case I have two images for dotnet having different labels and three images for our sample web application. It is showing three images for our application because, I have deployed it three times and you will notice that even though it's having different tags, it's using the same Image Id.

So we have created a ASP.NET Core web application in the local machine, published it to Docker directly from Visual Studio, hosted it in the Docker and then accessed it from the browser in the local machine.

Addendum : Troubleshooting issues during the building the application

While trying out this scenario, you may face some issues like the couples of one I encountered while trying this out.

The first error I got while trying to run the project was

Failed to run the command: "<project folder>\Docker\DockerTask.ps1 -Run -Enviroment Debug -Machine 'default' -OpenSite $False -RemoteDebugging $True", Click for more details.

This error normally occurs when you use Docker for Windows beta, so to fix this you need to edit the Docker.props file in the Properties folder.

<!-- Use this property to change the docker host that is used by this project. Delete default, leaving the value blank for the Docker for Windows beta
(Note: you need to restart VS after changing this property) -->
<DockerMachineName Condition=" '$(DockerMachineName)'=='' ">Default</DockerMachineName>

Remove the Default for the DockerMachineName tag and you need to restart Visual Studio to apply this change. My issue got resolved after this, but if you still facing the issue try restarting the machine.

So after fixing this, I got another issue which was,

The target process exited without raising a CoreCLR started event. Ensure that the target process is configured to use NETStandard 1.0.0-rc2-23901 or newer. This may be expected if the target process did not run .NET code.

This error will happen, if haven't setup volume mounting in Docker. Here docker is trying to access the libraries of your project which is in the local machine and you didn't gave access to the folder yet. For that, click on the Manage Shared Drives option from the Settings dialog and select the drive where your project folder resides.


No Comments

Add a Comment