Skip to content

Commit

Permalink
chore(docker): build image with ASP.NET Core support for Native AOT (…
Browse files Browse the repository at this point in the history
…~28.7MB)
  • Loading branch information
atrakic committed Jun 12, 2024
1 parent 0d854d1 commit 6acad98
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 51 deletions.
1 change: 0 additions & 1 deletion .envrc.ex → .envrc.sample
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,3 @@
# export ARM_TENANT_ID="<azure_subscription_tenant_id>"
# export ARM_CLIENT_ID="<service_principal_appid>"
# export ARM_CLIENT_SECRET="<service_principal_password>"

16 changes: 11 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
# https://mcr.microsoft.com/product/dotnet/sdk
# https://mcr.microsoft.com/v2/dotnet/sdk/tags/list
FROM --platform=$BUILDPLATFORM mcr.microsoft.com/dotnet/nightly/sdk:8.0-jammy-aot AS build
ARG TARGETARCH
WORKDIR /source

COPY src/*.csproj .
RUN dotnet restore
RUN dotnet restore -r linux-$TARGETARCH

COPY src/. .
RUN dotnet publish --no-restore -c Release -o /app

FROM mcr.microsoft.com/dotnet/aspnet:6.0 as final
RUN dotnet publish -r linux-$TARGETARCH --no-restore -o /app
RUN rm /app/*.dbg /app/*.Development.json

FROM mcr.microsoft.com/dotnet/nightly/runtime-deps:8.0.6-noble-chiseled-aot as final
WORKDIR /app
COPY --from=build /app .

ENV PORT 3000
EXPOSE ${PORT}

USER 1000
ENTRYPOINT ["dotnet", "demo-dotnet.dll"]
USER $APP_UID
ENTRYPOINT ["./demo-dotnet"]
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# azure-aks-dotnet-api-demo
This repository demonstrates how to deploy .Net 6.0 application on Azure Kubernetes(AKS) from scratch.
This repository demonstrates how to deploy .Net 8.0 application on Azure Kubernetes(AKS) from scratch.

## Requirements
* Azure account
Expand Down
33 changes: 24 additions & 9 deletions src/Program.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
using Prometheus;
using System.Text.Json.Serialization;

//var builder = WebApplication.CreateBuilder(args);
var builder = WebApplication.CreateSlimBuilder(args);

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddHealthChecks();
builder.Services.AddLogging();

builder.Services.ConfigureHttpJsonOptions(options =>
{
options.SerializerOptions.TypeInfoResolverChain.Insert(0, AppJsonSerializerContext.Default);
});

var app = builder.Build();

app.MapHealthChecks("/healthz");
app.UseMetricServer();
app.UseHttpLogging();
app.UseRouting();
app.UseHttpMetrics();

app.MapGet("/", () =>
{
var version = Environment.GetEnvironmentVariable("VERSION") ?? builder.Configuration["VERSION"];
var version = System.Environment.GetEnvironmentVariable("VERSION") ?? builder.Configuration["VERSION"];
return new Metadata(System.Net.Dns.GetHostName(), version);

Check warning on line 21 in src/Program.cs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest)

Possible null reference argument for parameter 'Version' in 'Metadata.Metadata(string Hostname, string Version)'.

Check warning on line 21 in src/Program.cs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest)

Possible null reference argument for parameter 'Version' in 'Metadata.Metadata(string Hostname, string Version)'.

Check warning on line 21 in src/Program.cs

View workflow job for this annotation

GitHub Actions / Build (windows-latest)

Possible null reference argument for parameter 'Version' in 'Metadata.Metadata(string Hostname, string Version)'.

Check warning on line 21 in src/Program.cs

View workflow job for this annotation

GitHub Actions / Build (windows-latest)

Possible null reference argument for parameter 'Version' in 'Metadata.Metadata(string Hostname, string Version)'.
}).WithName("GetMetadata");

Expand All @@ -28,5 +32,16 @@

app.Run();

record Metadata(string Hostname, string Version){}
record User(string name){}

public record Metadata(string Hostname, string Version);
public record User(string Name);

// https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/source-generation?pivots=dotnet-8-0

[JsonSourceGenerationOptions(PropertyNamingPolicy = JsonKnownNamingPolicy.KebabCaseLower)]
[JsonSerializable(typeof(Metadata))]
[JsonSerializable(typeof(User[]))]
[JsonSerializable(typeof(string))]
internal partial class AppJsonSerializerContext : JsonSerializerContext
{
}
38 changes: 11 additions & 27 deletions src/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -1,31 +1,15 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:33202",
"sslPort": 44359
}
},
"profiles": {
"user_demo_dotnet": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:7033;http://localhost:5073",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
"$schema": "http://json.schemastore.org/launchsettings.json",
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "releases",
"applicationUrl": "http://localhost:5132",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
}
1 change: 1 addition & 0 deletions src/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Use [native AOT deployment](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/native-aot?view=aspnetcore-8.0)
13 changes: 5 additions & 8 deletions src/demo-dotnet.csproj
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<RootNamespace>demo_dotnet</RootNamespace>
<PublishAot>true</PublishAot>
<InvariantGlobalization>true</InvariantGlobalization>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Data.Sqlite" Version="6.0.31" />
<PackageReference Include="prometheus-net" Version="5.0.1" />
<PackageReference Include="prometheus-net.AspNetCore" Version="5.0.1" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
<PackageReference Include="Microsoft.Data.Sqlite" Version="8.0.6" />
</ItemGroup>

</Project>

0 comments on commit 6acad98

Please sign in to comment.