.NET Quick Start
This document describes how to manually containerize a .NET application from scratch and deploy it to Tencent Cloud hosting (CloudBase Run).
Step 1: Write the Basic Application
Install .NET Core SDK 3.1. In the Console, use the dotnet command to create a new empty Web project:
dotnet new web -o helloworld-csharp
cd helloworld-csharp
Update the CreateHostBuilder
definition in Program.cs
to listen on port 80
:
using System;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
namespace helloworld_csharp
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args)
{
string port = "80";
string url = String.Concat("http://0.0.0.0:", port);
return Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>().UseUrls(url);
});
}
}
}
Update the content of Startup.cs
to the following:
using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace helloworld_csharp
{
public class Startup
{
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapGet("/", async context =>
{
await context.Response.WriteAsync("Hello World!\n");
});
});
}
}
}
The above code creates a basic Web server and listens on port 80
.
Step 2: Containerize the Application
In the project root directory, create a file named Dockerfile
with the following content:
# Use the Microsoft official .NET image as the build environment
# https://hub.docker.com/_/microsoft-dotnet-core-sdk/
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-alpine AS build
WORKDIR /app
# Install dependency.
COPY *.csproj ./
RUN dotnet restore
# Copy the local code to the container.
COPY . ./
WORKDIR /app
# Build the project.
RUN dotnet publish -c Release -o out
# Use the Microsoft official .NET image as the runtime image
# https://hub.docker.com/_/microsoft-dotnet-core-aspnet/
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-alpine AS runtime
WORKDIR /app
COPY /app/out ./
# Start the service.
ENTRYPOINT ["dotnet", "helloworld-csharp.dll"]
Add a .dockerignore
file to exclude files from the container image:
**/obj/
**/bin/
Step 3 (Optional): Build the image locally
If you have Docker installed locally, you can run the following command to build the Docker image locally:
docker build -t helloworld-csharp .
After a successful build, run docker images
to view the built image.
REPOSITORY TAG IMAGE ID CREATED SIZE
helloworld-csharp latest 1c8dfb88c823 8 seconds ago 105MB
Then you can upload this image to your image repository.
Run the following command to start the container:
docker run -p 80:80 helloworld-csharp
Access http://localhost
, you should see the "Hello World!" output.
Step 4: Deploy to CloudBase Cloud Run
If you have already installed the CloudBase CLI, you can use the following command in the project directory to deploy the application to CloudBase Cloud Hosting:
tcb cloudrun deploy
After entering the environment and service names, the CLI will automatically package the application image and deploy it to Cloud Hosting. For more deployment methods, refer to Deploying Services.