如何在 Blazor 应用程序的 docker 命令中更新连接链 (appsetting.json)?
How do I update the connection chain (appsetting.json) in a docker command for a Blazor app?
我想知道如何通过 docker 命令更新连接链。
Wordpress 示例:
docker run --name myWordpress -d -p 8080:80 -e WORDPRESS_DB_HOST=123.123.123.123:3306 -e WORDPRESS_DB_USER=rootUser -e WORDPRESS_DB_PASSWORD=password ....
我有一个需要连接到 MySQL 数据库的 blazor 服务器应用程序,这里是连接链 (appsetting.json)
"ConnectionStrings": {
"MySqlConnection": "server=YourDatabase;user id=username;password=YourPassword;database=yourDb"
},
我希望能够执行 docker 命令:
docker run -d -e server=123.123.123.123 -e uid=UserName -e password=MyPass ...
并更新文件 appsetting.json,但是如何?
感谢您的帮助。
我找到了解决办法。
我的appsetting.json
"ConnectionStrings": {
"MySqlConnection": "server=YOURDATABASE;user id=USERNAME;password=YOURPASSWORD;database=YOURDB"
}
在我的Startup.cs
public void ConfigureServices(IServiceCollection services)
{
string connectionDb = Configuration.GetConnectionString("MySqlConnection");
string databaseAddress = Environment.GetEnvironmentVariable("DB_HOST");
string login = Environment.GetEnvironmentVariable("LOGIN_DB");
string mdp = Environment.GetEnvironmentVariable("PASSWORD_DB");
string dbName = Environment.GetEnvironmentVariable("DB_NAME");
connectionDb = connectionDb.Replace("USERNAME", login)
.Replace("YOURPASSWORD", mdp)
.Replace("YOURDB", dbName)
.Replace("YOURDATABASE", databaseAddress);
services.AddDbContext<ApplicationDbContext>(options => options.UseMySql(connectionDb, ServerVersion.AutoDetect(connectionDb)));
// Service SQL de AccessData.
services.AddSingleton(new SqlContextAccess(connectionDb));
...
我的 Dockerfile
FROM mcr.microsoft.com/dotnet/aspnet:5.0-buster-slim AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
ENV DB_HOST="YourAddressdatabase"
ENV LOGIN_DB="YourLogin"
ENV PASSWORD_DB="YourPassword"
ENV DB_NAME="YourDbName"
...
和我的 docker 命令:
docker run -d -p 3030:80 \
-e LOGIN_DB=myLogin \
-e PASSWORD_DB=SuperPassword \
-e DB_NAME=nameOfMydb \
-e DB_HOST=123.123.123.123 \
--name test myImage:latest
我想知道如何通过 docker 命令更新连接链。 Wordpress 示例:
docker run --name myWordpress -d -p 8080:80 -e WORDPRESS_DB_HOST=123.123.123.123:3306 -e WORDPRESS_DB_USER=rootUser -e WORDPRESS_DB_PASSWORD=password ....
我有一个需要连接到 MySQL 数据库的 blazor 服务器应用程序,这里是连接链 (appsetting.json)
"ConnectionStrings": {
"MySqlConnection": "server=YourDatabase;user id=username;password=YourPassword;database=yourDb"
},
我希望能够执行 docker 命令:
docker run -d -e server=123.123.123.123 -e uid=UserName -e password=MyPass ...
并更新文件 appsetting.json,但是如何? 感谢您的帮助。
我找到了解决办法。
我的appsetting.json
"ConnectionStrings": {
"MySqlConnection": "server=YOURDATABASE;user id=USERNAME;password=YOURPASSWORD;database=YOURDB"
}
在我的Startup.cs
public void ConfigureServices(IServiceCollection services)
{
string connectionDb = Configuration.GetConnectionString("MySqlConnection");
string databaseAddress = Environment.GetEnvironmentVariable("DB_HOST");
string login = Environment.GetEnvironmentVariable("LOGIN_DB");
string mdp = Environment.GetEnvironmentVariable("PASSWORD_DB");
string dbName = Environment.GetEnvironmentVariable("DB_NAME");
connectionDb = connectionDb.Replace("USERNAME", login)
.Replace("YOURPASSWORD", mdp)
.Replace("YOURDB", dbName)
.Replace("YOURDATABASE", databaseAddress);
services.AddDbContext<ApplicationDbContext>(options => options.UseMySql(connectionDb, ServerVersion.AutoDetect(connectionDb)));
// Service SQL de AccessData.
services.AddSingleton(new SqlContextAccess(connectionDb));
...
我的 Dockerfile
FROM mcr.microsoft.com/dotnet/aspnet:5.0-buster-slim AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
ENV DB_HOST="YourAddressdatabase"
ENV LOGIN_DB="YourLogin"
ENV PASSWORD_DB="YourPassword"
ENV DB_NAME="YourDbName"
...
和我的 docker 命令:
docker run -d -p 3030:80 \
-e LOGIN_DB=myLogin \
-e PASSWORD_DB=SuperPassword \
-e DB_NAME=nameOfMydb \
-e DB_HOST=123.123.123.123 \
--name test myImage:latest