更新时间:2023-02-17 08:20:26
对于-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
vi
命令
docker exec -it 43ea835776dd /bin/bash
进入容器ls
列出文件并找到appsettings.json
vi appsettings.json
修改内容cat appsettings.json
检查内容是否已更改exit
并访问Home/Index
以检查结果.vi
command with apt-get install vim -y
docker exec -it 43ea835776dd /bin/bash
to go into container ls
to list files and find the appsettings.json
vi appsettings.json
to modify the contentcat appsettings.json
to check the content whether it is changedexit
and access the Home/Index
to check the result.