From 3ebb3ceaf44e964cd29caad9e133d8603589c5b4 Mon Sep 17 00:00:00 2001 From: wanggaofeng Date: Wed, 1 Dec 2021 17:56:46 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=A1=B9=E7=9B=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AppDemo/.dockerignore | 25 +++++++ AppDemo/AppDemo.sln | 25 +++++++ AppDemo/AppDemo/App.config | 3 + AppDemo/AppDemo/AppDemo.csproj | 33 +++++++++ AppDemo/AppDemo/Dockerfile | 20 ++++++ AppDemo/AppDemo/Program.cs | 68 ++++++++++++++++++ .../AppDemo/Properties/launchSettings.json | 13 ++++ AppDemo/AppDemo/appsettings.Development.json | 3 + AppDemo/AppDemo/appsettings.json | 12 ++++ AppDemo/Dockerfile | 20 ++++++ Demo/NetAppDemo/App/AppDemo.dll | Bin 5632 -> 6144 bytes .../App/appsettings.Development.json | 10 +-- Demo/NetAppDemo/App/appsettings.json | 3 +- Demo/NetAppDemo/Dockerfile | 8 ++- 14 files changed, 230 insertions(+), 13 deletions(-) create mode 100644 AppDemo/.dockerignore create mode 100644 AppDemo/AppDemo.sln create mode 100644 AppDemo/AppDemo/App.config create mode 100644 AppDemo/AppDemo/AppDemo.csproj create mode 100644 AppDemo/AppDemo/Dockerfile create mode 100644 AppDemo/AppDemo/Program.cs create mode 100644 AppDemo/AppDemo/Properties/launchSettings.json create mode 100644 AppDemo/AppDemo/appsettings.Development.json create mode 100644 AppDemo/AppDemo/appsettings.json create mode 100644 AppDemo/Dockerfile diff --git a/AppDemo/.dockerignore b/AppDemo/.dockerignore new file mode 100644 index 0000000..3729ff0 --- /dev/null +++ b/AppDemo/.dockerignore @@ -0,0 +1,25 @@ +**/.classpath +**/.dockerignore +**/.env +**/.git +**/.gitignore +**/.project +**/.settings +**/.toolstarget +**/.vs +**/.vscode +**/*.*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/AppDemo/AppDemo.sln b/AppDemo/AppDemo.sln new file mode 100644 index 0000000..9ba88ce --- /dev/null +++ b/AppDemo/AppDemo.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.31911.196 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AppDemo", "AppDemo\AppDemo.csproj", "{91A256A6-962E-4D61-887C-2E7BAD206002}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {91A256A6-962E-4D61-887C-2E7BAD206002}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {91A256A6-962E-4D61-887C-2E7BAD206002}.Debug|Any CPU.Build.0 = Debug|Any CPU + {91A256A6-962E-4D61-887C-2E7BAD206002}.Release|Any CPU.ActiveCfg = Release|Any CPU + {91A256A6-962E-4D61-887C-2E7BAD206002}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {92E0A98C-A330-485F-B3CA-F3DEC02824B2} + EndGlobalSection +EndGlobal diff --git a/AppDemo/AppDemo/App.config b/AppDemo/AppDemo/App.config new file mode 100644 index 0000000..49cc43e --- /dev/null +++ b/AppDemo/AppDemo/App.config @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/AppDemo/AppDemo/AppDemo.csproj b/AppDemo/AppDemo/AppDemo.csproj new file mode 100644 index 0000000..f3d2391 --- /dev/null +++ b/AppDemo/AppDemo/AppDemo.csproj @@ -0,0 +1,33 @@ + + + + Exe + net5.0 + Linux + + + + + + + + + + + + + + + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + + diff --git a/AppDemo/AppDemo/Dockerfile b/AppDemo/AppDemo/Dockerfile new file mode 100644 index 0000000..46376c7 --- /dev/null +++ b/AppDemo/AppDemo/Dockerfile @@ -0,0 +1,20 @@ +#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging. + +FROM mcr.microsoft.com/dotnet/runtime:5.0 AS base +WORKDIR /app + +FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build +WORKDIR /src +COPY ["AppDemo/AppDemo.csproj", "AppDemo/"] +RUN dotnet restore "AppDemo/AppDemo.csproj" +COPY . . +WORKDIR "/src/AppDemo" +RUN dotnet build "AppDemo.csproj" -c Release -o /app/buildQ-- + +FROM build AS publish +RUN dotnet publish "AppDemo.csproj" -c Release -o /app/publish + +FROM base AS final +WORKDIR /app +COPY --from=publish /app/publish . +ENTRYPOINT ["dotnet", "AppDemo.dll"] \ No newline at end of file diff --git a/AppDemo/AppDemo/Program.cs b/AppDemo/AppDemo/Program.cs new file mode 100644 index 0000000..95866cc --- /dev/null +++ b/AppDemo/AppDemo/Program.cs @@ -0,0 +1,68 @@ +using Microsoft.Extensions.Configuration; + +using System; +using System.Diagnostics; +using System.Threading; + +namespace AppDemo +{ + class Program + { + static void Main(string[] args) + { + var netcoreEnvironmentName = "DOTNET_ENVIRONMENT"; + var environmentValue = Environment.GetEnvironmentVariable(netcoreEnvironmentName); + + + var builder = new ConfigurationBuilder() + .SetBasePath(AppDomain.CurrentDomain.BaseDirectory) + .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) + .AddJsonFile($"appsettings.{environmentValue}.json", optional: true, reloadOnChange: true) + .AddEnvironmentVariables() + .AddCommandLine(args); + + var configurationRoot = builder.Build(); + + var jsonSerializerSettings = new Newtonsoft.Json.JsonSerializerSettings() + { + Formatting = Newtonsoft.Json.Formatting.Indented, + }; + + while (true) + { + Console.WriteLine("当前运行环境"); + Console.WriteLine($"运行模式:{(Debugger.IsAttached ? "调试模式" : "发布模式")}"); + Console.WriteLine($"args参数:{string.Join(",", args ?? new string[0])}"); + + var environmetValue = configurationRoot.GetValue(netcoreEnvironmentName); + Console.WriteLine($"配置项中,{netcoreEnvironmentName}={environmetValue}"); + + if (args != null) + { + foreach (var arg in args) + { + Console.WriteLine($"args传参数:{arg}"); + if (!string.IsNullOrWhiteSpace(arg)) + { + var argList = arg.Split('='); + if (argList.Length == 2) + { + Console.WriteLine($"配置管理中,{argList[0]}的值为:{configurationRoot.GetValue(argList[0])}"); + } + } + } + } + + Console.WriteLine("---------------------------------------------------------------------"); + Console.WriteLine(); + + //var configText = Newtonsoft.Json.JsonConvert.SerializeObject(configurationRoot.GetChildren(),jsonSerializerSettings); + //Console.WriteLine("当前配置:"); + //Console.WriteLine(configText); + //Console.WriteLine("---------------------------------------------------------------------"); + //Console.WriteLine(); + Thread.Sleep(5000); + } + } + } +} diff --git a/AppDemo/AppDemo/Properties/launchSettings.json b/AppDemo/AppDemo/Properties/launchSettings.json new file mode 100644 index 0000000..b4c1f6d --- /dev/null +++ b/AppDemo/AppDemo/Properties/launchSettings.json @@ -0,0 +1,13 @@ +{ + "profiles": { + "AppDemo": { + "commandName": "Project", + "environmentVariables": { + "DOTNET_ENVIRONMENT": "Development" + } + }, + "Docker": { + "commandName": "Docker" + } + } +} \ No newline at end of file diff --git a/AppDemo/AppDemo/appsettings.Development.json b/AppDemo/AppDemo/appsettings.Development.json new file mode 100644 index 0000000..7bd8da1 --- /dev/null +++ b/AppDemo/AppDemo/appsettings.Development.json @@ -0,0 +1,3 @@ +{ + "urls": ["https://www.baidu.com","cn.bing.com"] +} \ No newline at end of file diff --git a/AppDemo/AppDemo/appsettings.json b/AppDemo/AppDemo/appsettings.json new file mode 100644 index 0000000..deb7bd6 --- /dev/null +++ b/AppDemo/AppDemo/appsettings.json @@ -0,0 +1,12 @@ +{ + "NETCORE": "Development", + "urls": [ "https://www.baidu.com", "cn.bing.com" ], + "exclude": [ + "**/bin", + "**/bower_components", + "**/jspm_packages", + "**/node_modules", + "**/obj", + "**/platforms" + ] +} \ No newline at end of file diff --git a/AppDemo/Dockerfile b/AppDemo/Dockerfile new file mode 100644 index 0000000..8ad2984 --- /dev/null +++ b/AppDemo/Dockerfile @@ -0,0 +1,20 @@ +#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging. + +FROM mcr.microsoft.com/dotnet/runtime:5.0 AS base +WORKDIR /app + +FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build +WORKDIR /src +COPY ["AppDemo/AppDemo.csproj", "AppDemo/"] +RUN dotnet restore "AppDemo/AppDemo.csproj" +COPY . . +WORKDIR "/src/AppDemo" +RUN dotnet build "AppDemo.csproj" -c Release -o /app/build + +FROM build AS publish +RUN dotnet publish "AppDemo.csproj" -c Release -o /app/publish + +FROM base AS final +WORKDIR /app +COPY --from=publish /app/publish . +ENTRYPOINT ["dotnet", "AppDemo.dll"] \ No newline at end of file diff --git a/Demo/NetAppDemo/App/AppDemo.dll b/Demo/NetAppDemo/App/AppDemo.dll index bf67e4c28d9f1268b75d228fcb2424cafd746208..371f624ede0bc1104839c7d5908580d611e5a304 100644 GIT binary patch delta 1969 zcmZvdYiv|S6vzKFckkU-d%Jg+?v`T9wzY7j-3r!5ffj9n&@J`>%gZ9vTGqO?bj$75 z*nsS!6-#&s-k3lGMq;9ns1^D}P-9RdK@$T(WBlYJA;j1OJ0oK^Hxz+Xn3wd_xBnngY*~6@i z`Ry-Ro%(SR@ROa*YtkAvF@XLJi_q)QOfTT^_GJ;xY(kR8d(0eyHJ1dTkm(b4lx(Nf zK{O5WC&Q+nY;FdK$r*&KzFcByC>f+?j+GSh5Si8s!pZ~Eo`}=hLr$4$*&;TRdM6~u z$hF=h@Y0BLPH;cvV`jeSp4PZDlNMc;ZJi>SW_?0*oA*p*;N{&s;0OlPvL_6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Ay$@p+Kkxpfzo$*xMOblD2&gkjaoyQdG z{_-8J&Um(CMjG;rx#3S)_+ji1C z;STAmGU*hpyW^&H%w3uDXwQE3OlYTP>-1)9UEHg9K8AvtFE;5%#`D zcD@OvrjYQd2j5+sqwSou<*(OTS3NfGuYY{i*Rm(wb+%*KH!p`*U9@c8fnZ75R#b{9 ZoNBE4if?c_`DN7xYq=6WZTP3{e*>0nG#CH? diff --git a/Demo/NetAppDemo/App/appsettings.Development.json b/Demo/NetAppDemo/App/appsettings.Development.json index b9dea1e..7bd8da1 100644 --- a/Demo/NetAppDemo/App/appsettings.Development.json +++ b/Demo/NetAppDemo/App/appsettings.Development.json @@ -1,11 +1,3 @@ { - "urls": ["https://www.baidu.com","cn.bing.com"], - "exclude": [ - "**/bin", - "**/bower_components", - "**/jspm_packages", - "**/node_modules", - "**/obj", - "**/platforms" - ] + "urls": ["https://www.baidu.com","cn.bing.com"] } \ No newline at end of file diff --git a/Demo/NetAppDemo/App/appsettings.json b/Demo/NetAppDemo/App/appsettings.json index b9dea1e..deb7bd6 100644 --- a/Demo/NetAppDemo/App/appsettings.json +++ b/Demo/NetAppDemo/App/appsettings.json @@ -1,5 +1,6 @@ { - "urls": ["https://www.baidu.com","cn.bing.com"], + "NETCORE": "Development", + "urls": [ "https://www.baidu.com", "cn.bing.com" ], "exclude": [ "**/bin", "**/bower_components", diff --git a/Demo/NetAppDemo/Dockerfile b/Demo/NetAppDemo/Dockerfile index d2fff48..c00bc28 100644 --- a/Demo/NetAppDemo/Dockerfile +++ b/Demo/NetAppDemo/Dockerfile @@ -1,5 +1,7 @@ +# 直接复制发布文件 FROM mcr.microsoft.com/dotnet/runtime:5.0 AS base -WORKDIR /app -COPY ./publish . - +WORKDIR /app +ENV DOTNET_ENVIRONMENT="Dockerfile_env" +COPY ./App . +# CMD [ "DOTNET_ENVIRONMENT=Dockerfile_cmd" ] ENTRYPOINT [ "dotnet","AppDemo.dll" ] \ No newline at end of file