Deploy ASP.NET 5 Apps to Docker on Azure

NOTE: This post is part 4 of a series on developing and deploying cross-platform web apps with ASP.NET 5:

  1. Develop and Deploy ASP.NET 5 Apps on Mac OS X
  2. Develop and Deploy ASP.NET 5 Apps on Linux
  3. Deploy ASP.NET 5 Apps to Docker on Linux
  4. Deploy ASP.NET 5 Apps to Docker on Azure (this post)

Download instructions and code for this post here: https://github.com/tonysneed/Deploy-AspNet5-Azure-Docker.

Over the past few years, a phenomenon known as “the Cloud” has appeared. While the term is rather nebulous and can mean a number of different things, with regard to business applications it generally refers to a deployment model where apps run on servers provided by a third party that rents out computational resources, such as CPU cycles, memory and storage, on a pay-as-you-go basis. There are different service models for cloud computing, including infrastructure (IaaS), platform (PaaS) and software (Saas). In this post I’ll focus on the first option, infrastructure, which allows you set up Linux virtual machines where you can deploy Docker images with your ASP.NET 5 apps and all their dependencies. There are a number of players in the IaaS market, including Amazon Elastic Compute Cloud (EC2), Google Compute Engine (GCE) and Microsoft Azure, but I’ll show you how to deploy a Dockerized ASP.NET 5 app to Azure using Docker Hub, GitHub and the Docker Client for Windows.

azure-logo

So let’s start out with GitHub. The reason we’re starting here is that you can set up Docker Hub to link to a GitHub repository that contains a Dockerfile. When you push a commit to the GitHub repo, Docker Hub will build a new image for your app. What makes this a nice approach is that you get automated builds with continuous integration, and it’s easy to pull images from Docker Hub and run them on the Linux VM on Azure.

To demonstrate this I’ve created two repositories on GitHub. The first one is a simple console app: https://github.com/tonysneed/AspNet5-ConsoleApp. It contains three files: project.json, program.cs, and a Dockerfile.


FROM microsoft/aspnet:1.0.0-beta4

COPY . /app

WORKDIR /app

RUN ["dnu", "restore"]

ENTRYPOINT ["dnx", ".", "run"]

The second is a simple web app: https://github.com/tonysneed/AspNet5-WebApp. It also contains three files: project.json, startup.cs and a Dockerfile.


FROM microsoft/aspnet:1.0.0-beta4

COPY . /app

WORKDIR /app

RUN ["dnu", "restore"]

EXPOSE 5004

ENTRYPOINT ["dnx", ".", "kestrel"]

Next, you’re going to need to set up an account on Docker Hub. It’s free, and you can log in using your GitHub credentials. Then add an “Automated Build” which links to a GitHub repo.

dockerhub-auto-build

By far the easiest way to create a new Docker virtual machine in Azure is to use the Visual Studio 2015 Tools for Docker. Otherwise, you’re going to need to create the certificates manually and upload them to the Azure portal when adding the VM Extension for Docker. When I attempted this on the Azure portal, installing the Docker extension hung. But I didn’t experience a problem using the VS Docker Tools to create a Docker VM on Azure.

You’ll have to go through a few steps in Visual Studio before you can create the VM on Azure. First create a new Web Project, selecting one of the ASP.NET 5 templates. For our purposes, an empty web project will do just fine.

vs-new-proj-empty

Then right-click on the generated project and select “Publish” from the context menu. Under Profile, select Docker Containers, at which point you’ll be presented with a list of existing Azure Docker virtual machines. Simply click the New button to create a new Linux VM with Docker installed.

vs-docker-vms

Enter a unique DNS name, together with an admin user name and password. You can check the option to auto-generate Docker certificates, and the wizard will create the required certificates, configure Docker on the VM to use them, and copy the certificate and key files into the “.docker” folder under your user profile, so that you can use the same certificates to create additional virtual machines on Azure or elsewhere.

create-docker-vm-azure

I highly encourage you to check out the video series on Docker for .NET Developers, where you can learn more about the VS Docker tools, which can also generate a Dockerfile and build scripts for publishing an app to a Docker container on the VM you created on Azure.

What’s cool is that installing the tools will also give you the docker client for Windows, which you can use from a command prompt to build and run Docker images on the remote VM. You can start with the following command to display basic information, supplying the host name and port number specified when you created the VM.


docker --tls -H tcp://linux-docker.cloudapp.net:2376 info

docker-info

To keep from having to include the remote host address with every command, you can set the DOCKER_HOST environment variable.


set docker_host=tcp://linux-docker.cloudapp.net:2376

You can now use the docker client on the command line to run packages. If the package is not already installed locally, it will be pulled from Docker Hub. The following, for example, will simply print “Hello from Docker” to the console, along with a few other bits of information.


docker --tls run -t hello-world

You can also run Docker images which you have pulled into Docker Hub from GitHub. The following command will run an ASP.NET 5 console app that prints “Hello World” to the console.


docker --tls run -t tonysneed/aspnet5-consoleapp

If you want to get the latest version of the image from Docker Hub, simply execute a pull command.


docker --tls pull tonysneed/aspnet5-consoleapp

The following command will run a daemonized web app, mapping port 80 on the VM to port 5004 on the container.


docker --tls run -t -d -p 80:5004 tonysneed/aspnet5-webapp

This command will return a big long number, which you can use to output the container logs. The following will display “Started” if successful, otherwise it will list runtime exceptions.


docker --tls logs f2de092f14b67590ae4dc08cd3a453a28271de0a8f27e6d80ec356cbc5151d43

To list running processes, execute the ps command, which will list all the running containers.


docker --tls ps

[/code]

docker-ps

 

Now you can just open a browser with a URL that contains the fully qualified DNS name for the VM in Azure: http://linux-docker.cloudapp.net.

ie-webapp-azure

Congratulations! You have successfully deployed an ASP.NET 5 web app to a Docker container running on a Linux virtual machine in Azure. More importantly, you have configured Docker Hub to re-build the image whenever a commit is pushed to a linked GitHub repo, and you know how to pull that Docker image into the VM on Azure from the command line using the Docker Client for Windows. The Visual Studio Tools for Docker make it easy to create the Linux VM on Azure and generate certificates which you can use to create other Docker VM’s and which you can copy to other machines (both Windows and non-Windows) so that you can run Docker commands from there. All in all, a sweet story indeed.

 

Stay Informed

Sign up for the latest blogs, events, and insights.

We deliver solutions that accelerate the value of Azure.
Ready to experience the full power of Microsoft Azure?

Atmosera is thrilled to announce that we have been named GitHub AI Partner of the Year.

X