Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
279 views
in Technique[技术] by (71.8m points)

asp.net - How do I update the connection chain (appsetting.json) in a docker command for a Blazor app?

I would like to know how to update the connection chain from a docker command. Example for 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 ....

I have a blazor server application that needs to connect to a MySQL database, here's the connection chain (appsetting.json)

"ConnectionStrings": {
        "MySqlConnection": "server=YourDatabase;user id=username;password=YourPassword;database=yourDb"
    },

I'd like to be able to do a docker command:

docker run -d -e server=123.123.123.123 -e uid=UserName -e password=MyPass ...

and update the file appsetting.json, but How ? Thanks for help.

question from:https://stackoverflow.com/questions/65887673/how-do-i-update-the-connection-chain-appsetting-json-in-a-docker-command-for-a

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...