Reputation: 385
I'm working on .net core 3.1 service worker. I've create a docker template and I'm able to create and start my container. So now I need to create 2 copy of same service 1 on port 1111 and second to port 2222.
My appsettings.json has a variabile
{
....
Service :
{
Port: "1111"
}
If I start the first container I read from appsettings.json and port is set to 1111 so now I need to override port. I create a compose file:
version: '3.4'
services:
broker:
environment:
SERVICE__PORT: 9999
image: ${DOCKER_REGISTRY-}service1
build:
context: .
dockerfile: service1\Dockerfile
volumes:
- ./Settings/appsettings.docker.json:c:/app/appsettings.json
- ./Logs:c:/app/logs
But no varibile override. I add the
FROM mcr.microsoft.com/dotnet/sdk:3.1
WORKDIR /app
ENV SERVICE__PORT=1234
COPY . .
ENTRYPOINT ["dotnet", "service1.dll"]
If I debug my code I see a lot off provider and on EviromentSet I see my var. On CreateHostBuilder I add some part to add loggin
host.ConfigureLogging(
loggingBuilder =>
{
loggingBuilder.ClearProviders();
var configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build();
var loggerConfig = new LoggerConfiguration()
.ReadFrom.Configuration(configuration);
var logger = loggerConfig.CreateLogger();
loggingBuilder.AddSerilog(logger, dispose: true);
}
);
And when my service start I try to get value via configuration service
public Worker(ILogger<Worker> logger, IConfiguration configuration)
{
_logger = logger;
_configuration = configuration;
}
...
private async Task StartServer()
{
try
{
....
var port = _configuration["Service:Port"] == ""
? 9876
: Convert.ToInt32(_configuration["Service:Port"]);
....
}
I also try to mount volume via compose but not work if I run docker comand via -v switch but from VS2019 not work
Upvotes: 0
Views: 791
Reputation: 752
to support overriding of appsettings with environment variables you have to enable the environment variable support when you create the host builder.
var configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.AddEnvironmentVariables()
.Build();
Best regards, Daniel
Upvotes: 1