简体   繁体   English

asp.net 核心通过 ENV 变量覆盖连接字符串

[英]asp.net core override connection strings via ENV variables

I have an asp.net core API 2.2 implemented.我有一个 asp.net 核心 API 2.2 实现。 I have created a docker image.我创建了一个 docker 图像。 I would like to override connection strings in appsettings.json file.我想覆盖 appsettings.json 文件中的连接字符串。

Is there any way to do it?有什么办法吗? I tried via environment variables when I start the container with command docker container run -e "ConnectionStrings:DefaultConnection={...here goes the connection string}"当我使用命令docker container run -e "ConnectionStrings:DefaultConnection={...here goes the connection string}"启动容器时,我尝试通过环境变量

I have also builder.AddEnvironmentVariables();我还有builder.AddEnvironmentVariables(); in my Startup.cs but connection string in my appsetting.json is not replaced.在我的 Startup.cs 中,但我的 appsetting.json 中的连接字符串未被替换。

I checked it inside the container, appsetting.json is there but the values are not replaced.我在容器中检查了它,appsetting.json 在那里,但值没有被替换。

Any other way how to do such cases?任何其他方式如何做这种情况? Thx.谢谢。

appsetting.json never gets changed, however based on the hierarchy, variables values get overridden. appsetting.json从未更改,但是根据层次结构,变量值将被覆盖。 Take a look at the examples at https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-2.2#conventions . https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals/configuration/?view=aspnetcore-2.2#conventions上查看示例。 It works pretty well. 效果很好。 Perhaps just to be sure that you didn't break something because of command line execution, you can try to assign environment variables via docker-compose. 也许只是为了确保您不会因为命令行执行而中断某些操作,您可以尝试通过docker-compose分配环境变量。 The rule of thumb is that the pattern that you use for the "key" must match (with the help of __ instead of : )what you have in the json file, so that it gets overridden. 经验法则是,您用于“键”的模式必须与json文件中的模式匹配(在__而不是:的帮助下),以便它被覆盖。

For -e , it will override the system environment which will change the connectionstring when you retrive from code, it will not affect the content in appsettings.json . 对于-e ,它将覆盖系统环境,该环境将在您从代码中检索时更改连接appsettings.json ,并且不会影响appsettings.json的内容。

For example 例如

  1. Assume you have a appsettings.json like 假设您有一个类似的appsettings.json

     { "ConnectionStrings": { "DefaultConnection": "Server=(localdb)\\\\mssqllocaldb;Database=aspnet-TestDockerConfiguration-416C6FD2-3531-42D6-9EDE-18AC45901208;Trusted_Connection=True;MultipleActiveResultSets=true" }, "Logging": { "LogLevel": { "Default": "Warning" } }, "AllowedHosts": "*" } 
  2. Retrive the connectionstring by _configuration.GetConnectionString("DefaultConnection") like 通过_configuration.GetConnectionString("DefaultConnection")连接_configuration.GetConnectionString("DefaultConnection")例如

     public class HomeController : Controller { private readonly IConfiguration _configuration; public HomeController(IConfiguration configuration) { _configuration = configuration; } public IActionResult Index() { return Ok(_configuration.GetConnectionString("DefaultConnection")); //return View(); } } 
  3. For docker run -it -p 8888:80 dockerconfiguration , it will return "Server=(localdb)\\\\mssqllocaldb;Database=aspnet-TestDockerConfiguration-416C6FD2-3531-42D6-9EDE-18AC45901208;Trusted_Connection=True;MultipleActiveResultSets=true" for Index Action 对于docker run -it -p 8888:80 dockerconfiguration ,它将返回"Server=(localdb)\\\\mssqllocaldb;Database=aspnet-TestDockerConfiguration-416C6FD2-3531-42D6-9EDE-18AC45901208;Trusted_Connection=True;MultipleActiveResultSets=true"索引动作

  4. For docker run -it -p 8888:80 dockerconfiguration -e "ConnectionStrings:DefaultConnection"="testsqlstring" , it will return testsqlstring 对于docker run -it -p 8888:80 dockerconfiguration -e "ConnectionStrings:DefaultConnection"="testsqlstring" ,它将返回testsqlstring

  5. So, if you only want to override the value in appsettings.json during runtime, you just need to specify like Step 4 因此,如果您只想在运行时覆盖appsettings.json中的值, appsettings.json只需指定类似步骤4

If you prefer change the appsettings.json file in docker container, you could follow steps below 如果您想更改docker容器中的appsettings.json文件,可以按照以下步骤操作

  1. Install vi command with apt-get install vim -y apt-get install vim -y安装vi命令
  2. Run docker exec -it 43ea835776dd /bin/bash to go into container 运行docker exec -it 43ea835776dd /bin/bash进入容器
  3. Run ls to list files and find the appsettings.json 运行ls列出文件并找到appsettings.json
  4. Run vi appsettings.json to modify the content 运行vi appsettings.json修改内容
  5. Run cat appsettings.json to check the content whether it is changed 运行cat appsettings.json检查内容是否更改
  6. Run exit and access the Home/Index to check the result. 运行exit并访问Home/Index以检查结果。

Tried without the quotes for the ConnectionStrings section and it worked (on a PowerShell console.尝试不使用 ConnectionStrings 部分的引号,它工作正常(在 PowerShell 控制台上。

docker run -d -p 8080:80 --name letscosmosweb -e ConnectionStrings__ProductsDbContext=$key letscosmosweb:v1

$key variable contains the connection string $key变量包含连接字符串

If you're using docker-compose如果您使用的是docker-compose

Besides specifying the variable in .env , don't forget to also set it in environments section of the docker-compose.yml .除了在.env中指定变量外,不要忘记也在docker-compose.ymlenvironments部分中设置它。 It got me:)它让我:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM