Create and Deploy Docker Container of ASP.Net Core 1.0 Application to Azure Linux Web Application

In this tutorial, we are going to see how to create and deploy a Docker Image for an ASP.Net Core 1.0 Application. At the end of this tutorial we are going to deploy the Docker image to Azure Linux Web App. Docker helps us in creating packages for our application into standard images. These Docker images are ready to be deployed on Docker Containers and can run on any environment. A Docker Image is a read-only template with instructions for creating a Docker container. A Docker Container is the execution environment and it is created from a Docker image.

The power of Docker comes from its containers which wrap everything needed to run our application, for example code, runtime, dependencies, tools etc. This will ensure that our application will always run irrespective of hosting environment.

To get started I have setup following development environment.

  1. Microsoft Windows 10 Enterprise N (10.0.10586 Build 10586) x64
  2. Visual Studio Community 2017 RC 15.0.26020.0
  3. Dotnet Version: 1.0.0-preview4-004233 (this SDK is based on MSBuild, so no Project.json in the created application)
  4. Docker tools for Windows https://docs.docker.com/docker-for-windows/.

Open Command line prompt in administrator mode. Execute following commands:

cd C:\

mkdir DockerDotnetCoreApp

cd DockerDotnetCoreApp

image

Create new ASP.Net Core Application by using following commands.

dotnet new -t web

dotnet restore

image

Add the following code to_Layout.cshtml to display the description of OS it is running on at runtime.

<p>@System.Runtime.InteropServices.RuntimeInformation.OSDescription</p>

Build and run the application by running following commands.

dotnet build

dotnet run

image

image

Lets publish the application.

dotnet publish

image

Go to the project publish directory.

image

Now lets start dockerizing our application. Lets check the installed docker version.

image

To create a docker image, we need a DockerFile at the root of publish folder. A DockerFile is a file without any extension.

FROM microsoft/dotnet:1.0.1-runtime
WORKDIR /app
COPY . .
EXPOSE 5000/tcp
ENV ASPNETCORE_URLS
http://*:5000
ENTRYPOINT [“dotnet”, “DockerDotnetCoreApp.dll”]

 

image

Docker file will fetch the microsoft/dotnet:1.0.1-runtime image (provided by Docker hub, other images can be found at https://hub.docker.com/r/microsoft/dotnet/). It uses this image as the base image and create our new image. Second line will create a working directory and then copies all the contents from application’s publish folder to working directory. Fourth line exposes port 5000 of webserver and fifth line configures our application to run under kestrel at 5000 port. The last line defines entry point with the command to be executed.

NOTE: This approach of creating a Docker Image (using base image and a Docker file) which is demonstrated below is one of many approaches. Much better approach has been demonstrated in other tutorial (Continuous Integration and Deployment of ASP.Net Core 1.0 Docker Image to Azure Ubuntu Virtual Machine using GitHub and Docker Cloud). Below approach will give the ability to run already compiled ASP.Net Core DLL, while the other approach will restore, build and run the ASP.Net Core 1 application at runtime. Having said the pros and cons, both approaches can be used subjectively depending on requirements.

To create an image, execute following command.

docker build . -t dockerdotnetcoreapp

image

To see list of all images created, we can execute following command. We see both the images, dockerdotnetcoreapp is the image we created and other is the Microsoft’s base image which has been used to create our image.

docker images

image

To create and run a docker container with the image which we created above, use the following command. The –p option maps port 5000 of container to our host machine’s port 8000.

docker run -p 8000:5000 dockerdotnetcoreapp

image

Navigate to localhost:8000, we should the application running on Linux environment.

image

To list of containers it created. execute below command.

docker ps

image

We have our image created, now lets deploy our container to Linux Web App (an Azure offering for running web applications on Linux environments). First we need to create an account at https://hub.docker.com/. We are going to push our newly created Docker image to Docker hub. Then we configure Linux Web App to use our image from Docker hub.

We will first login to Docker hub from command line using below command.

docker login

image

Our image should follow username/imagename format, otherwise it cannot be pushed to Docker hub. Use below command to create a image with proper naming convention.

docker tag dockerdotnetcoreapp ramiramilu/dockerdotnetcoreapp

Then we will push our image from local to Docker hub using below command.

docker push ramiramilu/dockerdotnetcoreapp

Our image will be pushed to Docker. Intermediate status will be displayed as below.

image

Once push is completed, we will have following status.

image

Pushed image is displayed on Docker Hub Dashboard.

image

Now lets create a Linux Web App at Azure.

image

And configure Docker image for Linux web app as shown below.

image

Click ok, and then click create. Once created we need to configure newly created Linux Web App to open 5000 Port (as we configured our image to open 5000 Port).

image

Save the settings. Now browse the site and we should see our application is up and running.

image

So we finally made our ASP.Net Core app up and running on Azure Linux Web App having Ubuntu OS using Docker Containers.

That’s it for now. Happy Coding and Stay Tuned!!!

You may also like...