RyptHunterz
RyptHunterz

Reputation: 55

Sending parameter by docker-compose does not work on appsettings in .NET6

I have a docker-compose with environment variables for the ConnectionString :

services:
  nameservice:
  ...
  environment:
    ConnectionStrings__mysqlDatabase: "Server=db;Uid=root;Pwd=password;"

I have on the .NET side in a console program, an AppSettings file (.NET6):

{
  "ConnectionStrings": {
    "mysqlDatabase": "Server=localhost;Uid=root;Pwd=password;"
  }
}

and my program.cs :

IConfiguration Config = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build();
ConnectionStringConstant.mysqlDatabase = Config.GetConnectionString("mysqlDatabase");

my problem is that in docker after creating a container, it keeps the value in AppSettings (which is not good for production) and it does not take the value passed by the docker-compose file.

Can you help me?

Upvotes: 3

Views: 2125

Answers (1)

Guru Stron
Guru Stron

Reputation: 142923

When building configuration manually (without using hosting) setup for environment variables support is required:

var configurationRoot = new ConfigurationBuilder()
    .AddJsonFile("appsettings.json")
    .AddEnvironmentVariables()
    .Build();

Read more:

Upvotes: 1

Related Questions