.net core 使用docker env变量启动
使用worker service 开发了一个程序。
用systemd已经可以正常运行。
然后想在docker运行,但是docker 中不能每次涉及到配置文件内容都去改json文件,还有密码啥的。太麻烦。
于是用docker env 变量来操作配置文件。
以下为代码:
FROM mcr.microsoft.com/dotnet/core/runtime:3.1-buster-slim AS base
WORKDIR /app
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /src
COPY ["Dockerstudy/Dockerstudy.csproj", "Dockerstudy/"]
RUN dotnet restore "Dockerstudy/Dockerstudy.csproj"
COPY . .
WORKDIR "/src/Dockerstudy"
RUN dotnet build "Dockerstudy.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "Dockerstudy.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
ENV hello=wuzy #增加参数
ENV test=docker1.1 #增加参数
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Dockerstudy.dll"]
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
var hello = Environment.GetEnvironmentVariable("hello",EnvironmentVariableTarget.Process);
var test = Environment.GetEnvironmentVariable("test", EnvironmentVariableTarget.Process);
Console.Write($"Hello1Process {hello},{test}!");
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((hostingContext, config) =>
{
config.AddEnvironmentVariables();
});
docker run -e test="test1111" hello="docker"