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:
1 2 3 4 5 6 7 8 9 10 11 12 |
FROM microsoft/dotnet:latest COPY . /app WORKDIR /app RUN dotnet restore RUN dotnet build EXPOSE 5000/tcp ENV ASPNETCORE_URLS http://*:5000 ENV ASPNETCORE_ENVIRONMENT docker ENTRYPOINT dotnet run |
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:
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:
1 2 3 4 5 6 7 8 9 10 11 |
server { listen 80; location / { proxy_pass http://localhost:5000; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; } } |
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.
Thanks! I wait for next part – how to use Rancher, how to configure load balancer etc.
Thanks :). I’ll give it a thought and hopefully record something about this topic in a near future.
Pingback: ASP.NET Core deployment using Docker, Nginx and Ubuntu Server - How to Code .NET
Pingback: Dew Drop - June 12, 2017 (#2498) - Morning Dew
Thanks for another great article. Where can anyone get that kind of information written in such a perfect manner? I have a presentation next week, and I’m on the search for such information.
Server 2016 Migration Experts
Hi,
Thanks, what exactly do you need?
This is one technology that I would love to be able to use for myself. It’s definitely a cut above the rest and I can’t wait until my provider has it. Your insight was what I needed. Thanks
Server 2016 Migration Experts
This is the first time I came here. I, your blog, especially its discussion of the stuff is very entertaining. From the tons of comments on your articles, I guess I’m not the only one having all the enjoyment! Keep up the good work.
Server 2016 Migration Experts
This is very serious, You’re associate passing qualified author. I actually have signed up along with your feed and in addition can relish finding your personal marvellous write-ups. Incidentally, we’ve shared your net journal in our social networks.
Server 2016 Migration Experts
Pingback: Hosting Docker images on Azure Container Registry | Piotr Gankiewicz
This is really serious, You’re an exceedingly qualified writer. I have signed up with your feed and additionally will enjoy finding your personal wonderful write-ups. Incidentally, We have shared your web blog in our social networks. plz visit the site
Server 2016 Migration Experts
Have you succeeded in using nginx as a HTTPS terminator?
Like User nginx kestrel and make kestrel thing the request schema is https. For using external login modules.
What exactly do you mean? I managed to configure HTTPS redirection etc. using Nginx, but I’m not sure what’s your goal.
Being quite new to using and deploying stuff to linux this was a great tutorial. I was following few guides from Linode and Docker but kept getting lost and this was exactly what I needed.
Będę musiał częściej odwiedzać twoją stronę.
Wielkie dzięki.
Happy to hear that, dzięki!