.net core 使用docker env变量启动

.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"
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值