Deploy ASP.Net 5 Applications in Linux Containers With Docker

Introduction

We know that ASP.NET 5 provides cross-platform development. We can now create or make an application using ASP.NET 5 and run this application on Android, Linux and Mac OSs. Microsoft introduced the first official Docker image to deploy an application on the Linux virtual machine. So in this article we learn how we create an application in ASP.NET 5 and deploy the application on the Linux virtual machine using Docker. Now the first question is, what is Docker? So let's talk about Docker.

Docker

Docker was introduced by Microsoft on 15 October 2014 .Docker is a Microsoft tool available as open source. The main thing Docker does is to Build, Ship and run applications on any platform. It is a very popular open source tool that provides an environment for developers and system administrators to create and run a distributed application. If we have a Docker enabled application then this application is assembled from the individual and for a unique component. Now companies currently that are usually related to the IT field use Docker because it can ship and run the same app in various operating systems including the Microsoft Azure cloud service.



We will now learn how to create an application in ASP.NET 5 and deploy it to a Linux Virtual Machine ruining on the Microsoft cloud Azure using Docker. This application will be run on any type of the operating system but one thing that is necessary is that on that specific system Docker is installed. If Docker is not available then you are not able to run the command on that operating system. Now follow the given instructions.

Step 1

We know that at this time Docker works only for the Linux right now so we need a machine on which the Linux operating system is installed or that machine at which Linux Virtual Machine should be available for running the Docker on our server. There is a procedure necessary for the installation of the Docker, you can find those instructions from installing the Docker or use the Docker for the Linux virtual machine from the Docker for the Microsoft Azure. So here we will assume you have a machine on which the Linux operating system is available with Docker. We will now move to the our other steps.

Step 2

To deploy our application on the cloud or Azure we need a container, so first we create a container image. In this container image is our application. Docker containers images available at the top position in the available positions in the containers. So here your base image will be in the container images microsoft/aspnet. The image layers will be stored at the various folders while you deploy your application. One thing notable here is that when we deploy our application it does not contain any ASP.NET binaries nor a Linux distribution file, it only contains those files to be used in your application. it only makes them smaller in size and through that they are easily deployed on the server. For creating the Docker images we use a file named "Dockerfile". In this Docker file there are some instructions responsible for building the image. In other words we can also say that this file specifies for Docker how to build the image.

  • git clone [email protected]:aspnet/Home.git aspnet-Home
  • cd aspnet-Home/samples/HelloWeb
This directory has some files as in the following structure:
  • startup.cs
  • image.jpg
  • project.json

Now we create a file in this directory. The name of this file, as I said above, is Dockerfile and it contains the following contents.

  • FROM microsoft/aspnet
  • COPY ./app
  • WORKDIR /app
  • RUN ["kpm", "restore"]
  • EXPOSE 5004
  • ENTRYPOINT ["k", "kestrel"]

Now here we discuss this line by line. The contents of a Docker file has six files and every file has importance so let's discuss the contents.

The first content FROM microsoft/aspnet tells Docker that we will using the official ASP.Net image for Docker.

The second content is COPY that tells Docker we use the contents of this folder for the /app directory and copy this contents to the /app directory folder.

The third content WORKDIR provides the Container that the data moves in the /app directory file.

The fourth content RUN tells Docker to please run the kpm restore command to install all the dependencies for the application. But ensure that the this command is always run before running the application for the first time.

The fifth content EXPOse tells Docker that the specific image uses a service and that service is listed at the port number 5004.

The last content ENCRYPTION provides the command to start the container and will be responsible for running the container continuously without any error using the kestral command.

Step 3

Now after performing the preceding steps our Dockfile is ready for use. Now our directory will be changed to something like the following:

  • Dockerfile
  • startup.cs
  • image.jpg
  • project.json

Now our next work is to build the Docker image. To build the Docker image we need to run a command. The command is given below. This command will be run in the directory.

docker build -t myapp

When we run this command in our directory it will build an image in Dockerfile. This is known as the myapp, now whenever you change it in your application then every time a new image will be built in your Dockerfile with the preceding command. If we want to see our application in the list of the Docker images in our Linux virtual machine then run the following command.

$ docker images



Here we can see that our application and ASP.Net images are available at the our directory. Now we can deploy our application at the Cloud.

Step 4

Now our next step to run the container. When we run the container we use the following command at the Docker in your Linux virtual machine.

docker run -t -d -p 80:5004 myapp

Here the -t is a switch to attach a pseudo for the container.

Here -d runs the container in the background if there is any process running in the foreground.

Here the -p switch is used for the mapping for the port 80 to virtual machine 5004 container port.

Here the connections are available at the port 80 forwarded for the container port 5004.

Fnally myapp is the Docker image we will use to start the container. Now our container is in the staring position, if we want to see the information of the running container then we use the following command:

$ docker ps

Here you have the information like this:



Now our Container is in the running position. Now here we do some other task. We just completed the endpoint port mapping for Azure VM. So go to the Azure official website and map the public port number 80 to the internal port number 80 for our Linux virtual machine. Because we run the our application at the our local host. now run our application at the browser that is enable at our machine. Run the following URL in your browser "http://your-cloud-service-name.cloudapp.net:80/".



Here our application is running successfully in the Linux virtual machine using Docker.

Summary

This is the overall idea behind the Linux virtual machine and the Docker files. I hope this article will be helpful for the reader to make an application in the Linux environment using Docker. The Docker is the Part of the ASP.NET 5. So maybe Microsoft will make some changes in the future for the Docker implementation. Docker is very time saving and it is most important to build an app and run this app anywhere with any operating system quickly.


Similar Articles