I want to build and host a website using my C# skill set. I could go to Microsoft Azure, but I don’t want to pay. I heard that the new Dot Net Core can run on Docker, and Synology supports Docker. Hmm sounds interesting, but how do I get to work? This blog shows you how.
What is .Net Core?
Net Core is the new development platform of Microsoft. It does not replace the traditional .Net but it coexists with .Net.
It has many exciting features:
- Open Source: maintained by Microsoft and the .Net community on GitGub.
- Modular structure: all packages are available on NuGet.
- Cross platform: Runs on Windows, Mac OS and Linux.
- Docker support: .Net Core images are available on Docker Hub.
What is Docker?
Docker is a lightweight virtualization application that gives you the ability to run containers on your NAS. You can build own container or download one from Docker Hub. Docker has several advantages:
- Light weight, you can run multiple docker images on one machine.
- Fast cold start.
- Easy deployment.
- Portability among several platforms, Max, Linux and Windows.
Synology is powered by Linux and and if your NAS has enough power you can run Docker. Check Docker on Synology if your model is supported.
Steps to take
- Install Docker.
- Create Dot Net Core application.
- Publish solution to NAS.
- Build Docker image.
- Launch and configure Docker image.
- Configure reverse proxy, this is optional.
Installing Docker on the Synology NAS is the next step. Open the Packer Center and search for Docker:
Docker will create a folder “<volumename>/docker” during installation. The volume name depends on your NAS configuration. The folder is hidden by default! It took me some time to figure out this folder was created. We will use this folder as the root folder for our Docker applications.
Create Dot Net Core application
There are several ways how you can build a .Net Core application. I prefer VS 2015 community edition with the .Net Core stack, it is feature-rich and free. Make sure you install the .Net Core Tools preview. You can find all the required setup files .Net Core here. The application itself is not the main goal for this blog, so I keep it simple and create a HelloWord MVC application.
and choose Web Application
For now we keep it simple and choose No Authentication
I hit F5 en confirm the “HelloWord ” application is running:
Publish solution to NAS
The next step is to publish the solution to the NAS. You can accomplish this in many ways, just make sure the files are copied to “<yournas>/docker/helloword”. The publish option does just that for us:
Clicking “Publish” will compile and copy the deployment files to \\<yournas>\docker\helloworld
Build Docker Image
Building a Docker images take some steps:
- Pull image from Docker repository.
- Create Dockerfile.
- Build Docker image with a telnet session.
We need a “base” image upon we can merge our files. There are lots of images available at Docker hub. You easily get confused which one to take. In our case we need an image with only the asp.net core libraries. This is a light weight image and has a very good cold start performance. Open the Docker application on your NAS and search for “microsoft/aspnetcore”. Select “1.0.1” as tag.
After completion you will see the downloaded image.
The “Dockerfile” is a file without a file extension. It is a kind of batch file with Docker commands. The file is needed during the image build process. You can create the file with any text editor you like. Save the file in folder \\<yournas>\docker\helloworld
COPY . /app
ENV ASPNETCORE_URLS http://*:7500
ENTRYPOINT dotnet webApp.dll
The Dockerfile execute several important steps :
- microsoft/aspnetcore:1.0.1 will be used for the helloword image.
- Sets the HelloWorld files location.
- Sets application IP port.
- Sets application entry point.
I add the Dockerfile to the project solution. In this way the file does not get deleted during publishing and the publish option (right click) makes it very easy to copy the file to the docker folder.
Build Docker image with a telnet session
This step merges the published files and the downloaded image into one new image. This new image will be launched and is the new application, so we are almost there. Steps:
- Start telnet <hostname> from your windows machine.
- Login with user name and password. Please note both password and username are case sensitive.
- telnet commands:
docker build -t helloworld .
sudo -i requires a root level account. In my setup the first sudo login attempt always fails. Please keep one space between helloword and the dot! Otherwise the build command will fail. Docker builds the new image, you can check the the result in telnet terminal and in the Docker management application on your NAS.
Launch and configure Docker image
All the hard work is now done. We only need to launch the image. During the launch process we must hook port 7500 to this image.
- Click Launch (see previous image).
- Click Advanced settings and select tab “Port Settings”.
- Enter “7500” to the local port.
- Complete the wizard.
The container menu shows that the application is running in our newly created container!
Start the application in your browser, do not forget the port number in the URL.
YES! it is all working.
Configure reverse proxy
Everything is now working. The only drawback is that the user has to know the port number. This can be annoying especially when there are multiple sites hosted. With a reverse proxy you can redirect a port number to a sub domain. Synology has this feature and it is easy to setup:
- Go to the Control Panel.
- Select Application Portal.
- Select tab Reverse Proxy.
- Create New.
- Set values.
Start your browser and enter the complete URL with the sub domain. It confirms everything is working as expected.
Nasty side effect reverse proxy
The reverse proxy gives a nasty side effect, which is not so obvious. Application internal IP-address are mapped to the 172.x.x.x segment. This occurs for example if you use a database connection, for example to SQL or MySQL. If these applications has firewall limitations on incoming IP-addresses your application will fail!
Dot Net Core and Docker provide excellent ways to deploy your application to different platforms. You can build the application with your current C# skill set. Deploying takes some learning but it is worth the effort. Please leave a reply for your comments.