SiteHost

Working with .NET Core Web Containers

You can deploy and run .NET Core web applications on Cloud Containers using the .NET Core + SDK Web images.

In this article, we will cover setting up a NET Core + SDK Web Container and customize it to run an example application. The overall process is:

  1. Provision the NET Core + SDK Web Container through the SiteHost Control Panel
  2. Connect to the Container created and build a .NET Core project
  3. Publish the web application
  4. Adjust the Container configuration to start the application on boot
  5. Test the application

Provisioning

We have an article on Creating a Container available if you are not familiar with this step. The process is the same for any Web Container. Please specify an SSH Username and SSH Password or associate an existing SFTP user with the Container once it's created. While creating the Web Container, you can also create a MySQL database if you plan to use it.

Create and build the app

For this exercise, we use the Get started with ASP.NET Core MVC documentation page as a reference but adapt it showcase the tool available to you on the NET Core + SDK image. The workflow is simple:

  1. Log in to the Container through SSH
  2. Create a project using the mvc template
  3. Remove the existing example application
  4. Publish the project into the /container/application directory
# ssh myuser@123.123.123.123
$ dotnet new mvc --no-https --exclude-launch-settings -f net6.0 -o CustomApp
$ rm -rf /container/application/*
$ dotnet publish -c release --no-restore CustomApp/ -o /container/application/

For this exercise, we use CustomApp as the project name, this means a directory with this name should have been created and the project files should be under ~/CustomApp. The published application files should reside in the /container/application/ directory.

Configuring the Container

Once the application is published, we can go ahead and configure Supervisor to start-up our application on boot. The supervisord.conf configuration file has a section named [program:dotnetapp] we can use it as a reference.

$ nano /container/config/supervisord.conf

We can comment the line to replace the default command for this program so we start the application we just created. Here is what the configuration file should look like:

[program:dotnetapp]
directory=/container/application/
;command=/usr/bin/dotnet ExampleApp.dll
command=/usr/bin/dotnet CustomApp.dll
stdout_logfile=/container/logs/supervisor/%(program_name)s-stdout.log
stderr_logfile=/container/logs/supervisor/%(program_name)s-stderr.log

Apply the changes and test

To apply all these changes, we must reboot the Container, all we need to do is click a few buttons on the SiteHost Control Panel.

Once the Container starts up again, check the website on a browser. In case things won't work as expected, you can follow the dotnetapp program logs and debug through your SSH session.

tail -f /container/logs/supervisor/dotnetapp-std*

After every change to the project files you want to deploy, you must publish the application and restart the Container through the SiteHost Control Panel. If you keep the same project name, you won't need to update the Supervisor configuration.

Alternative workflow

The application project files do not need to be stored on the Cloud Container, and you should be able to publish the application on your workstation. In most cases, developers build and test locally on a workstation using an Integrated Development Environment (IDE) and that's what we'd recommend doing to avoid unnecessary load on your Cloud Container server.

Once you are happy with the application developed and are ready to publish it, remember the Cloud Containers are based on the Linux Operating System so ensure your application is published to run on Linux. You can read more about the options available on the .NET fundamentals "dotnet publish" documentation page.

dotnet publish -c release --runtime linux-x64

After publishing your application, upload it to the /container/application/ directory on the Container and make any required changes to the supervisord.conf configuration file. As always, reboot the Container to apply the changes.