• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Evaluaciones
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

1.6K
Vistas
asp.net core override connection strings via ENV variables

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.

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}"

I have also builder.AddEnvironmentVariables(); in my Startup.cs but connection string in my appsetting.json is not replaced.

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.

about 3 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

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.

For example

  1. Assume you have a appsettings.json like

    {
        "ConnectionStrings": {
            "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=aspnet-TestDockerConfiguration-416C6FD2-3531-42D6-9EDE-18AC45901208;Trusted_Connection=True;MultipleActiveResultSets=true"
        },
        "Logging": {
            "LogLevel": {
            "Default": "Warning"
            }
        },
        "AllowedHosts": "*"
    }
    
  2. 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();
        }
    }
    
  3. 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

  4. For docker run -it -p 8888:80 dockerconfiguration -e "ConnectionStrings:DefaultConnection"="testsqlstring", it will return testsqlstring

  5. So, if you only want to override the value in appsettings.json during runtime, you just need to specify like Step 4

If you prefer change the appsettings.json file in docker container, you could follow steps below

  1. Install vi command with apt-get install vim -y
  2. Run docker exec -it 43ea835776dd /bin/bash to go into container
  3. Run ls to list files and find the appsettings.json
  4. Run vi appsettings.json to modify the content
  5. Run cat appsettings.json to check the content whether it is changed
  6. Run exit and access the Home/Index to check the result.
about 3 years ago · Santiago Trujillo Denunciar

0

appsetting.json never gets changed, however based on the hierarchy, variables values get overridden. Take a look at the examples at https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-2.2#conventions . It works pretty well. Perhaps just to be sure that you didn't break something because of command line execution, you can try to assign environment variables via docker-compose. The rule of thumb is that the pattern that you use for the "key" must match (with the help of __ instead of :)what you have in the json file, so that it gets overridden.

about 3 years ago · Santiago Trujillo Denunciar

0

Tried without the quotes for the ConnectionStrings section and it worked (on a PowerShell console.

docker run -d -p 8080:80 --name letscosmosweb -e ConnectionStrings__ProductsDbContext=$key letscosmosweb:v1

$key variable contains the connection string

about 3 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda