Note: Before going to read this article, I would suggest to read Docker Simplified article that will help you understanding the concept of docker and how it works however if you are familiar with docker you can easily continue reading.
.NET Core is a general purpose development platform maintained by Microsoft and the .NET community on GitHub. It is cross-platform, supporting Windows, macOS and Linux, and can be used in device, cloud, and embedded/IoT scenarios. It gives the abilities to run .net apps and websites in different operating systems, by default Kestrel web server is embedded to run ASP.NET sites there.
Let’s start by pulling an image from docker’s hub that embeds .NET Core
docker pull microsoft/dotnet
The following line will create a new container from dotnet image and run it in an interactive mode or in other words let you get inside the container as if it is like a virtual machine (BUT it is not actually a virtual machine) to run commands
docker run -ti -p 80:5000 -v $(pwd):/var/www microsoft/dotnet
Now Let’s install generator (named Yo) that will help us generate a new template for our project (it is exactly like creating new project in Visual Studio). It will ask you which type of application you need to generate, choose “Web Application” and choose “Bootstrap” as the styling library for your website.
[email protected]:/# apt-get update -y [email protected]:/# curl -sL https://deb.nodesource.com/setup_6.x | sh - [email protected]:/# apt-get install -y nodejs [email protected]:/# npm install yo generator-aspnet -g [email protected]:/# cd /var/www [email protected]:/# yo aspnet Empty Web Application (F#) Console Application Console Application (F#) ❯ Web Application Web Application Basic [without Membership and Authorization] Web Application Basic [without Membership and Authorization] (F#)
For more info regarding installing nodejs in different platforms visit https://nodejs.org/en/download/package-manager/
Hint: we can build an image (that is having by now nodejs, npm, yo, generator-aspnet) to use later as our new base image to avoid the above operation in the future by using the following example that builds a new image named “msoliman/dotnet”, this image now encapsulate all necessary softwares we need. for more information about Docker, check Docker Simplified and Most Common Docker Commands
docker commit containerId|containerName imageTagName docker commit containerId msoliman/dotnet
Now it’s time to make a restore operation that will grab and install all required dependencies and packages that makes website working.
Let’s build and run the our application
dotnet build => optional, as run will automatically build before running dotnet run
Please notice that the port 5000 is the default port to run in kestrel server with dotnet core however it is not accepting all connection from outside as it is listening internally to localhost’s requests only we can do one of the following to avoid any problems:
You can pass this option during running your application
dotnet run --server.urls = "http://0.0.0.0:5000"
or you can use method named “UseUrls” when building the web host inside Program.cs, it should be looking like the following, notice we can make more than one address/port:
var host = new WebHostBuilder() .UseUrls("http://*:1000","http://0.0.0.0:5000") .UseKestrel() .UseContentRoot(Directory.GetCurrentDirectory()) .UseIISIntegration() .UseStartup<Startup>() .Build(); host.Run();c
Now try access your application in your localhost if you are using docker engine directly in your host
If you are using a docker machine, first get its ip address first using the following:
docker-machine ip machineName 192.168.99.100
Then your site should be available at http://192.168.99.100:80
You should see something like the following: