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

我有ASP.NET 2.0的webAPI和使用Mysql数据库。所以我在application.json文件中configuration“ConnectionString”。当我在本地运行它时,我能够读取连接string,并且应用程序正在运行完美。但是我试图使用Docker工具箱部署到docker工人。我写了三个文件:

  1. Dockerfile
  2. DOckerComposer
  3. Proxy.Conf

我正在使用docker composer文件,因为我必须在ngnix服务器上运行。我能够构build映像并运行它。 现在我正试图覆盖从docker-compose文件的连接string,但它不会被覆盖。

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)); }