如何读取环境变量在ASP.NET核心2.0 + Docker中?

时间:2017-09-21 09:06:03

标签: asp.net docker environment-variables docker-compose dockerfile

我有ASP.NET核心2.0 webAPI并使用Mysql数据库。所以我正在配置" ConnectionString"在application.json文件中。当我在本地运行它时,我能够读取连接字符串并且应用程序正常运行。但是当我尝试使用Docker工具箱部署到docker时,情况并非如此。我写了三个文件: -

  1. Dockerfile
  2. DockerCompose
  3. Proxy.Conf
  4. 我正在使用docker compose文件,因为我必须在ngnix服务器上运行。我能够构建图像,并运行它。现在我试图覆盖docker-compose文件中的连接字符串,但它没有被覆盖。

    DockerFile

    FROM microsoft/aspnetcore:2.0
    ARG source
    WORKDIR /app
    COPY $source .
    EXPOSE 5000
    ENTRYPOINT ["dotnet", "xx.dll"]
    

    搬运工-compose.yaml

    version: '2'
    services:
        app:
            container_name: cp-api
            build: .
            environment:
            - "ConnectionStrings:Test=server=192.168.99.101; port=3306; user=root; password=X123; database=TestDb; TreatTinyAsBoolean=true;"
        rev-proxy:
            container_name: rev-proxy
            image: nginx
            ports:
             - "9090:8080"
            volumes:
             - ./proxy.conf:/etc/nginx/conf.d/default.conf`
    

    Proxy.conf

    server {
        listen 8080;
    
        location / {
          proxy_pass http://cp-api:5000;
        }
    }
    

    appsetting.json: -

    {
      "ConnectionStrings": {
        "EcallDbNextGen": "server =192.168.99.100; port =3306; user =root; password =xxx; database =Testdb; TreatTinyAsBoolean=true;"
      },
      "Logging": {
        "IncludeScopes": false,
        "Debug": {
          "LogLevel": {
            "Default": "Warning"
          }
        },
        "Console": {
          "LogLevel": {
            "Default": "Warning"
          }
        }
      }
    }
    

    Startup.cs

    public Startup(IConfiguration configuration, ILoggerFactory loggerFactory) {
         Configuration = configuration;
         loggerFactory.AddConsole(Configuration.GetSection("Logging")); //log levels set in your configuration
         loggerFactory.AddDebug(); //does all log levels
         //removed rest of code
     }
    
     public IConfiguration Configuration {
         get;
     }
    
     // This method gets called by the runtime. Use this method to add services to the container.
     public void ConfigureServices(IServiceCollection services) {
    
         var connection = Configuration.GetConnectionString("EcallDbNextGen");
         services.AddDbContext < ecallnextgendbContext > (options => options.UseMySql(connection));
     }
    

0 个答案:

没有答案