I found a solution.
My appsetting.json
"ConnectionStrings": {
"MySqlConnection": "server=YOURDATABASE;user id=USERNAME;password=YOURPASSWORD;database=YOURDB"
}
In my 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));
...
My 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"
...
and my docker command :
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
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…