Click here to Skip to main content
15,868,016 members
Articles / Containers / Docker

Run .Net Core apps in Docker on Synology

Rate me:
Please Sign up or sign in to vote.
4.85/5 (11 votes)
22 Jan 2017CPOL5 min read 56.4K   33   12
How to run .Net Core Apps in a Docker Container on a Synology nas.

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

  1. Install Docker.
  2. Create Dot Net Core application.
  3. Publish solution to NAS.
  4. Build Docker image.
  5. Launch and configure Docker image.
  6. Configure reverse proxy, this is optional.

Install Docker

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:

  1. Pull image from Docker repository.
  2. Create Dockerfile.
  3. Build Docker image with a telnet session.

Pull image

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.

Create Dockerfile

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

Content Dockerfile:

FROM microsoft/aspnetcore:1.0.1

COPY . /app
WORKDIR /app

ENV ASPNETCORE_URLS http://*:7500
EXPOSE 7500

ENTRYPOINT dotnet webApp.dll

The Dockerfile execute several important steps :

  1. microsoft/aspnetcore:1.0.1 will be used for the helloword image.
  2. Sets the HelloWorld files location.
  3. Sets application IP port.
  4. Sets application entry point.

Tip 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:

  1. Start telnet <hostname> from your windows machine.
  2. Login with user name and password. Please note both password and username are case sensitive.
  3. Telnet commands:
sudo -i

cd /volume1/docker/helloworld

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.

  1. Click Launch (see previous image).
  2. Click Advanced settings and select tab "Port Settings".
  3. Enter "7500" to the local port.

  4. 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:

  1. Go to the Control Panel.
  2. Select Application Portal.
  3. Select tab Reverse Proxy.
  4. Create New.
  5. 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!

Conclusion

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.

Further reading

Setup Dot Net Core Development Docker
Docker Wiki
Configure Telnet on Synology

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead
Netherlands Netherlands
I graduated as Bachelor of Mechanical Engineering. Soon I moved from mechanical to software engineering. With more than 20 years of experience in software design, development, and architecture I love building software that users enjoy en suit their needs.

Comments and Discussions

 
QuestionImage not showing Pin
apiegoku29-Jan-24 23:51
apiegoku29-Jan-24 23:51 
QuestionImages gone to nirvana? Pin
dkurok31-Oct-17 5:36
dkurok31-Oct-17 5:36 
AnswerRe: Images gone to nirvana? Pin
Bart-Jan Brouwer6-Nov-17 9:46
Bart-Jan Brouwer6-Nov-17 9:46 
GeneralRe: Images gone to nirvana? Pin
Lauryx1-May-19 1:19
professionalLauryx1-May-19 1:19 
PraiseThank you for this great article! :) Pin
Member 1208864315-Jun-17 9:46
Member 1208864315-Jun-17 9:46 
PraiseExtremely useful Pin
leparduk21-Jan-17 6:24
leparduk21-Jan-17 6:24 
QuestionAlso posted as blog Pin
Wendelius19-Jan-17 3:25
mentorWendelius19-Jan-17 3:25 
AnswerRe: Also posted as blog Pin
Bart-Jan Brouwer19-Jan-17 3:36
Bart-Jan Brouwer19-Jan-17 3:36 
AnswerRe: Also posted as blog Pin
Bart-Jan Brouwer8-Feb-17 20:38
Bart-Jan Brouwer8-Feb-17 20:38 
QuestionFormatting issues Pin
Pete O'Hanlon18-Jan-17 4:48
subeditorPete O'Hanlon18-Jan-17 4:48 
AnswerRe: Formatting issues Pin
Bart-Jan Brouwer18-Jan-17 19:18
Bart-Jan Brouwer18-Jan-17 19:18 
GeneralRe: Formatting issues Pin
Pete O'Hanlon18-Jan-17 21:00
subeditorPete O'Hanlon18-Jan-17 21:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.