65852

asp.net core override connection strings via ENV variables

<h3>Question</h3>

I have an asp.net core API 2.2 implemented. I have created a docker image. I would like to override connection strings in appsettings.json file.

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}"

I have also builder.AddEnvironmentVariables(); in my Startup.cs but connection string in my appsetting.json is not replaced.

I checked it inside the container, appsetting.json is there but the values are not replaced.

Any other way how to do such cases? Thx.


<h3>Answer1:</h3>

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.

For example

<ol><li>

Assume you have a appsettings.json like

{ "ConnectionStrings": { "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=aspnet-TestDockerConfiguration-416C6FD2-3531-42D6-9EDE-18AC45901208;Trusted_Connection=True;MultipleActiveResultSets=true" }, "Logging": { "LogLevel": { "Default": "Warning" } }, "AllowedHosts": "*" } </li> <li>

Retrive the connectionstring by _configuration.GetConnectionString("DefaultConnection") like

public class HomeController : Controller { private readonly IConfiguration _configuration; public HomeController(IConfiguration configuration) { _configuration = configuration; } public IActionResult Index() { return Ok(_configuration.GetConnectionString("DefaultConnection")); //return View(); } } </li> <li>

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

</li> <li>

For docker run -it -p 8888:80 dockerconfiguration -e "ConnectionStrings:DefaultConnection"="testsqlstring", it will return testsqlstring

</li> <li>

So, if you only want to override the value in appsettings.json during runtime, you just need to specify like Step 4

</li> </ol>

If you prefer change the appsettings.json file in docker container, you could follow steps below

<ol><li>Install vi command with apt-get install vim -y</li> <li>Run docker exec -it 43ea835776dd /bin/bash to go into container </li> <li>Run ls to list files and find the appsettings.json</li> <li>Run vi appsettings.json to modify the content</li> <li>Run cat appsettings.json to check the content whether it is changed</li> <li>Run exit and access the Home/Index to check the result.</li> </ol>
<h3>Answer2:</h3>

appsetting.json never gets changed, however based on the hierarchy, variables values get overridden. Take a look at the examples at https://docs.microsoft.com/en-us/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. 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.

来源:https://stackoverflow.com/questions/56465059/asp-net-core-override-connection-strings-via-env-variables

Recommend

  • Parsing city of origin / destination city from a string
  • Angular Elements EventEmmitter : $event not showing emitted value
  • framework not found FirebaseInstanceID in Xcode
  • Position thumbnails on bottom with Fancybox
  • Registry class. If key exist
  • How to achieve the below goal in obiee dashboard
  • How to test each date field in the array response using Karate
  • Android 1.5 Multitouch
  • How to write RequireJS loader plugin with cache?
  • Python SQL database query giving “Too few parameters” error
  • HTML5 datalist tag is not working in android
  • Object reference not set to an instance of an object
  • Validation controls in ASP.NET are run at server- or client-side?
  • Invalid LOC header(bad signature)
  • How to shuffle an array of objects in javascript?
  • Passing Structs in Ctypes
  • Emoji to Hex value in ios
  • PHP curl: “Unknown cipher in list”
  • What is the effect of having an empty while loop?
  • Firebase notifications are not sending as high priority
  • Hash character in URLs (accessing and redirecting in Apache)
  • How to make a JavaScript object's prototype permanent?
  • Jenkins to run Maven build on Linux or Windows
  • smarter character replacement using ruby gsub and regexp
  • Conditional spread element
  • Lambda inside loop
  • AWS Trust Policy Has prohibited field Principal
  • JS insert into array at specific index [duplicate]
  • Fastest way to convert IEnumerable to List in C#
  • Add click to imagebutton inside gridview dynamically
  • getsockname return invalid address
  • .Net core Hosted Services guaranteed to complete
  • opencv deskewing a contour
  • How to define a custom accuracy in Keras to ignore samples with a particular gold label?
  • Jersey serializes character value to ASCII equivalent numeric string