ASP.NET Core deployment using Docker, Nginx and Ubuntu Server

ASP.NET Core deployment using Docker, Nginx and Ubuntu Server

Since ASP.NET Core became a truly cross-platform framework, we’re free to use other environments such as Linux in order to host our applications. This is a great opportunity not only to reduce the possible licensing costs but also to try out a new environment. In the video tutorial below, I’ll show you how to build a Docker image using ASP.NET Core, publish it to the Virtual Machine running in the Digital Ocean and use Nginx to expose the app to the world.

 

1. Creating a sample web application

At the very beginning let’s create a sample ASP.NET Core application using the available template. It does not matter whether you choose the MVC or Web API.

Just type dotnet new mvc, then dotnet restore and eventually dotnet run to make sure that the application works under the default localhost:5000 URL.

2. “Dockerizing” ASP.NET Core application

In order to build the Docker image create a new Dockerfile within the root directory of a project:

And type the following command: docker build -t webapp-demo .
Having this in place, you can run the Docker container simply by executing:
docker run -p 5000:5000 webapp-demo.

Please note that for the production purposes, you should probably use the another Dockerfile that would be built using the already deployed files using dotnet publish and ENTRYPOINT dotnet APP_NAME.dll.

3. Docker Hub registry

Docker Hub is a registry for the Docker Images (like a GitHub). You can publish the public images for free or choose the paid version for the private repositories. There’s an available integration with both, GitHub and BitBucket which means that can we can have the automated build in place once the commit was pushed to the source code repository. For the simplicity of this tutorial, I chose the already existing image on my own repository.

4. Deploying app to the Ubuntu Server

I created a Virtual Machine (called Droplet since I’m using Digital Ocean cloud) containing the Ubuntu Server 16.04. Once you access the VM using ssh, you should install the Docker, for example by following this guide. Then, you can pull and run the actual Docker image – in my case I simply typed docker pull spetz/net-core-tour-2017-demo and then docker run -d -p 5000:5000 spetz/net-core-tour-2017-demo.

Beware of the -d argument – if you choose not to run the Docker container in the background, the only to stop it will be by logging to the VM once again and executing docker stop PID where PID is the identifier that can be found by executing docker ps command.
Now, you should be able to access the application under the http://VM_IP_ADDRESS:5000 URL (add /entries endpoint if you’re using my sample Docker image).

5. Configuring Nginx

Nginx is a great and very popular (and also powerful) HTTP server. Well, the Kestrel itself is also great and blazingly fast, however, it’s not suited for the production environment. Things like SSL or setting up a load balancer are not really possible, which means that you should go with a server like IIS, Apache, Nginx or so. In our case we will stick to the Nginx – in order to install it, type the following command: apt-get install nginx. Try to access the http://VM_IP_ADDRESS URL – you should see the default Nginx site.

The next step is to actually configure the Nginx using a technique called reverse proxy, which will redirect all of the incoming movement from a given port e.g 80 to the internal port e.g. 5000 where our application runs. Let’s navigate to the /etc/nginx/sites-enabled directory, type the rm default and then nano default in order to create a new configuration file containing the following settings:

Eventually, execute the service nginx restart command and that’s it – your web application running within the Docker container should be accessible from the public port of 80. If you’d like to restrict the ports, you can use the UWF and type in commands like ufw allow 80.

13 Comments ASP.NET Core deployment using Docker, Nginx and Ubuntu Server

  1. Pingback: ASP.NET Core deployment using Docker, Nginx and Ubuntu Server - How to Code .NET

  2. Pingback: Dew Drop - June 12, 2017 (#2498) - Morning Dew

  3. Pingback: Hosting Docker images on Azure Container Registry | Piotr Gankiewicz

Leave A Comment

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