asp.net核心通过ENV变量覆盖连接字符串 [英] asp.net core override connection strings via ENV variables
问题描述
我实现了一个asp.net核心API 2.2.我已经创建了一个docker镜像.我想覆盖appsettings.json文件中的连接字符串.
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.
有什么办法吗?当我使用命令docker container run -e "ConnectionStrings:DefaultConnection={...here goes the connection string}"
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}"
我的Startup.cs中也有builder.AddEnvironmentVariables();
,但是appsetting.json中的连接字符串未替换.
I have also builder.AddEnvironmentVariables();
in my Startup.cs but connection string in my appsetting.json is not replaced.
我在容器中检查了它,appsetting.json在其中,但未替换值.
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.
推荐答案
对于-e
,它将覆盖系统环境,该环境将在您从代码检索时更改连接字符串,不会影响appsettings.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
.
例如
-
假设您有一个
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": "*"
}
通过
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();
}
}
对于docker run -it -p 8888:80 dockerconfiguration
,它将为索引操作返回"Server=(localdb)\\mssqllocaldb;Database=aspnet-TestDockerConfiguration-416C6FD2-3531-42D6-9EDE-18AC45901208;Trusted_Connection=True;MultipleActiveResultSets=true"
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 -e "ConnectionStrings:DefaultConnection"="testsqlstring"
,它将返回testsqlstring
因此,如果您只想在运行时覆盖appsettings.json
中的值,则只需指定类似步骤4
So, if you only want to override the value in appsettings.json
during runtime, you just need to specify like Step 4
如果您希望更改docker容器中的appsettings.json文件,可以按照以下步骤
If you prefer change the appsettings.json file in docker container, you could follow steps below
- 使用
apt-get install vim -y
安装 - 运行
docker exec -it 43ea835776dd /bin/bash
进入容器 - 运行
ls
列出文件并找到appsettings.json
- 运行
vi appsettings.json
修改内容 - 运行
cat appsettings.json
检查内容是否已更改 - 运行
exit
并访问Home/Index
以检查结果.
vi
命令
- Install
vi
command withapt-get install vim -y
- Run
docker exec -it 43ea835776dd /bin/bash
to go into container - Run
ls
to list files and find theappsettings.json
- Run
vi appsettings.json
to modify the content - Run
cat appsettings.json
to check the content whether it is changed - Run
exit
and access theHome/Index
to check the result.
这篇关于asp.net核心通过ENV变量覆盖连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!