Docker 容器中的 dotnet 应用程序看不到环境变量

dotnet app in Docker container not seeing environment variables

我有一个工作的 dotnet 应用程序,我可以在本地 运行,同样的代码 运行s 在 azure web 应用程序中。我已经能够将它容器化。但是,当我 运行 它在容器中时,它无法读取环境变量:

控制器中 get/check 环境变量的代码:

        public ReportController(ILogger<ReportController> logger, IConfiguration iconfig)
        {
            _logger = logger;
            _config = iconfig;
            _storageConnString = Environment.GetEnvironmentVariable("AzureWebJobsStorage");
            _containerName = Environment.GetEnvironmentVariable("ReportContainer");
            string CredentialConnectionString = Environment.GetEnvironmentVariable("CredentialConnectionString");
            if(CredentialConnectionString == null)
            {
                throw new Exception("Credential connection string is null");
            }
        }

启动代码:

public static void Main(string[] args)
        {
           CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                })
                .ConfigureAppConfiguration((hostingContext, config) =>
                {
                    config.AddEnvironmentVariables();
                });
    ```

my docker compose 正在设置变量:


services:
  myreports:
    image: myreports    
    build:
      context: .
      dockerfile: myreports/Dockerfile
    ports: [5000:5000]
    environment:    
      - "APPSETTINGS_AzureWebJobsStorage = DefaultEndpointsProtocol=https;AccountName=mystorage;AccountKey=xxxx+xx/xx==;EndpointSuffix=core.windows.net"

      - "APPSETTINGS_HarmonyConnectionString = Data Source=mydb.database.windows.net;AttachDbFilename=;Initial Catalog=Harmony;Integrated Security=False;Persist Security Info=False;User ID=sqlreporter;Password=mypass"
      - "APPSETTINGS_CredentialConnectionString = Data Source=mydb.database.windows.net;AttachDbFilename=;Initial Catalog=Credential;Integrated Security=False;Persist Security Info=False;User ID=sqlreporter;Password=mypass"
      - "CredentialConnectionString = Data Source=mydb.database.windows.net;AttachDbFilename=;Initial Catalog=Credential;Integrated Security=False;Persist Security Info=False;User ID=sqlreporter;Password=mypass"
      - "APPSETTINGS_ReportContainer = taxdocuments"

如您所见,我同时尝试了 APPSETTINGS_ 前缀而不是

但是当我点击应用程序中的端口时,容器 returns:

myreports-1  | System.Exception: Credential connection string is null

代码在获取变量的应用服务中运行良好

不需要在变量名前加上APPSETTINGS_。导致问题的原因是 docker-compose 文件中等号周围的空格。不需要引号,所以我会删除它们。

这应该有效

services:
  myreports:
    image: myreports    
    build:
      context: .
      dockerfile: myreports/Dockerfile
    ports: [5000:5000]
    environment:    
      - AzureWebJobsStorage=DefaultEndpointsProtocol=https;AccountName=mystorage;AccountKey=xxxx+xx/xx==;EndpointSuffix=core.windows.net
      - HarmonyConnectionString=Data Source=mydb.database.windows.net;AttachDbFilename=;Initial Catalog=Harmony;Integrated Security=False;Persist Security Info=False;User ID=sqlreporter;Password=mypass
      - CredentialConnectionString=Data Source=mydb.database.windows.net;AttachDbFilename=;Initial Catalog=Credential;Integrated Security=False;Persist Security Info=False;User ID=sqlreporter;Password=mypass
      - ReportContainer=taxdocuments