From 1758837f0a601ab7a25979d77cb1ca1e5cf4680d Mon Sep 17 00:00:00 2001 From: yao Date: Sun, 17 Nov 2024 11:58:16 +0800 Subject: [PATCH] add docker example --- Docker/Lab.Nexus.Docker/.dockerignore | 25 +++++++++++ .../Lab.Nexus.Docker.WebApi/Dockerfile | 23 ++++++++++ .../Lab.Nexus.Docker.WebApi.csproj | 21 +++++++++ .../Lab.Nexus.Docker.WebApi.http | 6 +++ .../Lab.Nexus.Docker.WebApi/Program.cs | 44 +++++++++++++++++++ .../Properties/launchSettings.json | 41 +++++++++++++++++ .../appsettings.Development.json | 8 ++++ .../Lab.Nexus.Docker.WebApi/appsettings.json | 9 ++++ Docker/Lab.Nexus.Docker/Lab.Nexus.Docker.sln | 21 +++++++++ Docker/Lab.Nexus.Docker/docker-compose.yml | 6 +++ 10 files changed, 204 insertions(+) create mode 100644 Docker/Lab.Nexus.Docker/.dockerignore create mode 100644 Docker/Lab.Nexus.Docker/Lab.Nexus.Docker.WebApi/Dockerfile create mode 100644 Docker/Lab.Nexus.Docker/Lab.Nexus.Docker.WebApi/Lab.Nexus.Docker.WebApi.csproj create mode 100644 Docker/Lab.Nexus.Docker/Lab.Nexus.Docker.WebApi/Lab.Nexus.Docker.WebApi.http create mode 100644 Docker/Lab.Nexus.Docker/Lab.Nexus.Docker.WebApi/Program.cs create mode 100644 Docker/Lab.Nexus.Docker/Lab.Nexus.Docker.WebApi/Properties/launchSettings.json create mode 100644 Docker/Lab.Nexus.Docker/Lab.Nexus.Docker.WebApi/appsettings.Development.json create mode 100644 Docker/Lab.Nexus.Docker/Lab.Nexus.Docker.WebApi/appsettings.json create mode 100644 Docker/Lab.Nexus.Docker/Lab.Nexus.Docker.sln create mode 100644 Docker/Lab.Nexus.Docker/docker-compose.yml diff --git a/Docker/Lab.Nexus.Docker/.dockerignore b/Docker/Lab.Nexus.Docker/.dockerignore new file mode 100644 index 00000000..cd967fc3 --- /dev/null +++ b/Docker/Lab.Nexus.Docker/.dockerignore @@ -0,0 +1,25 @@ +**/.dockerignore +**/.env +**/.git +**/.gitignore +**/.project +**/.settings +**/.toolstarget +**/.vs +**/.vscode +**/.idea +**/*.*proj.user +**/*.dbmdl +**/*.jfm +**/azds.yaml +**/bin +**/charts +**/docker-compose* +**/Dockerfile* +**/node_modules +**/npm-debug.log +**/obj +**/secrets.dev.yaml +**/values.dev.yaml +LICENSE +README.md \ No newline at end of file diff --git a/Docker/Lab.Nexus.Docker/Lab.Nexus.Docker.WebApi/Dockerfile b/Docker/Lab.Nexus.Docker/Lab.Nexus.Docker.WebApi/Dockerfile new file mode 100644 index 00000000..52aa1b4e --- /dev/null +++ b/Docker/Lab.Nexus.Docker/Lab.Nexus.Docker.WebApi/Dockerfile @@ -0,0 +1,23 @@ +FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base +USER $APP_UID +WORKDIR /app +EXPOSE 8080 +EXPOSE 8081 + +FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build +ARG BUILD_CONFIGURATION=Release +WORKDIR /src +COPY ["Lab.Nexus.Docker.WebApi/Lab.Nexus.Docker.WebApi.csproj", "Lab.Nexus.Docker.WebApi/"] +RUN dotnet restore "Lab.Nexus.Docker.WebApi/Lab.Nexus.Docker.WebApi.csproj" +COPY . . +WORKDIR "/src/Lab.Nexus.Docker.WebApi" +RUN dotnet build "Lab.Nexus.Docker.WebApi.csproj" -c $BUILD_CONFIGURATION -o /app/build + +FROM build AS publish +ARG BUILD_CONFIGURATION=Release +RUN dotnet publish "Lab.Nexus.Docker.WebApi.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false + +FROM base AS final +WORKDIR /app +COPY --from=publish /app/publish . +ENTRYPOINT ["dotnet", "Lab.Nexus.Docker.WebApi.dll"] diff --git a/Docker/Lab.Nexus.Docker/Lab.Nexus.Docker.WebApi/Lab.Nexus.Docker.WebApi.csproj b/Docker/Lab.Nexus.Docker/Lab.Nexus.Docker.WebApi/Lab.Nexus.Docker.WebApi.csproj new file mode 100644 index 00000000..137df212 --- /dev/null +++ b/Docker/Lab.Nexus.Docker/Lab.Nexus.Docker.WebApi/Lab.Nexus.Docker.WebApi.csproj @@ -0,0 +1,21 @@ + + + + net8.0 + enable + enable + Linux + + + + + + + + + + .dockerignore + + + + diff --git a/Docker/Lab.Nexus.Docker/Lab.Nexus.Docker.WebApi/Lab.Nexus.Docker.WebApi.http b/Docker/Lab.Nexus.Docker/Lab.Nexus.Docker.WebApi/Lab.Nexus.Docker.WebApi.http new file mode 100644 index 00000000..83f85e02 --- /dev/null +++ b/Docker/Lab.Nexus.Docker/Lab.Nexus.Docker.WebApi/Lab.Nexus.Docker.WebApi.http @@ -0,0 +1,6 @@ +@Lab.Nexus.Docker.WebApi_HostAddress = http://localhost:5133 + +GET {{Lab.Nexus.Docker.WebApi_HostAddress}}/weatherforecast/ +Accept: application/json + +### diff --git a/Docker/Lab.Nexus.Docker/Lab.Nexus.Docker.WebApi/Program.cs b/Docker/Lab.Nexus.Docker/Lab.Nexus.Docker.WebApi/Program.cs new file mode 100644 index 00000000..37519f38 --- /dev/null +++ b/Docker/Lab.Nexus.Docker/Lab.Nexus.Docker.WebApi/Program.cs @@ -0,0 +1,44 @@ +var builder = WebApplication.CreateBuilder(args); + +// Add services to the container. +// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle +builder.Services.AddEndpointsApiExplorer(); +builder.Services.AddSwaggerGen(); + +var app = builder.Build(); + +// Configure the HTTP request pipeline. +if (app.Environment.IsDevelopment()) +{ + app.UseSwagger(); + app.UseSwaggerUI(); +} + +app.UseHttpsRedirection(); + +var summaries = new[] +{ + "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" +}; + +app.MapGet("/weatherforecast", () => + { + var forecast = Enumerable.Range(1, 5).Select(index => + new WeatherForecast + ( + DateOnly.FromDateTime(DateTime.Now.AddDays(index)), + Random.Shared.Next(-20, 55), + summaries[Random.Shared.Next(summaries.Length)] + )) + .ToArray(); + return forecast; + }) + .WithName("GetWeatherForecast") + .WithOpenApi(); + +app.Run(); + +record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary) +{ + public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); +} \ No newline at end of file diff --git a/Docker/Lab.Nexus.Docker/Lab.Nexus.Docker.WebApi/Properties/launchSettings.json b/Docker/Lab.Nexus.Docker/Lab.Nexus.Docker.WebApi/Properties/launchSettings.json new file mode 100644 index 00000000..2f894152 --- /dev/null +++ b/Docker/Lab.Nexus.Docker/Lab.Nexus.Docker.WebApi/Properties/launchSettings.json @@ -0,0 +1,41 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:51845", + "sslPort": 44311 + } + }, + "profiles": { + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "swagger", + "applicationUrl": "http://localhost:5133", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "swagger", + "applicationUrl": "https://localhost:7167;http://localhost:5133", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "swagger", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/Docker/Lab.Nexus.Docker/Lab.Nexus.Docker.WebApi/appsettings.Development.json b/Docker/Lab.Nexus.Docker/Lab.Nexus.Docker.WebApi/appsettings.Development.json new file mode 100644 index 00000000..0c208ae9 --- /dev/null +++ b/Docker/Lab.Nexus.Docker/Lab.Nexus.Docker.WebApi/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/Docker/Lab.Nexus.Docker/Lab.Nexus.Docker.WebApi/appsettings.json b/Docker/Lab.Nexus.Docker/Lab.Nexus.Docker.WebApi/appsettings.json new file mode 100644 index 00000000..10f68b8c --- /dev/null +++ b/Docker/Lab.Nexus.Docker/Lab.Nexus.Docker.WebApi/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/Docker/Lab.Nexus.Docker/Lab.Nexus.Docker.sln b/Docker/Lab.Nexus.Docker/Lab.Nexus.Docker.sln new file mode 100644 index 00000000..cfa847b0 --- /dev/null +++ b/Docker/Lab.Nexus.Docker/Lab.Nexus.Docker.sln @@ -0,0 +1,21 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{DB1C35E6-C552-4CB5-9D4D-8B999DEB279B}" + ProjectSection(SolutionItems) = preProject + docker-compose.yml = docker-compose.yml + EndProjectSection +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lab.Nexus.Docker.WebApi", "Lab.Nexus.Docker.WebApi\Lab.Nexus.Docker.WebApi.csproj", "{DBE5CBDD-78A6-4A49-A301-6FFF8249E27B}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {DBE5CBDD-78A6-4A49-A301-6FFF8249E27B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {DBE5CBDD-78A6-4A49-A301-6FFF8249E27B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {DBE5CBDD-78A6-4A49-A301-6FFF8249E27B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {DBE5CBDD-78A6-4A49-A301-6FFF8249E27B}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal diff --git a/Docker/Lab.Nexus.Docker/docker-compose.yml b/Docker/Lab.Nexus.Docker/docker-compose.yml new file mode 100644 index 00000000..29707c0d --- /dev/null +++ b/Docker/Lab.Nexus.Docker/docker-compose.yml @@ -0,0 +1,6 @@ +services: + lab.nexus.docker.webapi: + image: lab.nexus.docker.webapi + build: + context: . + dockerfile: Lab.Nexus.Docker.WebApi/Dockerfile