Enviroment variable from docker-compose to .net core app

Hello. I want to pass my enviroment variable(API_ENDPOINT) from docker-compose to my .net core API app.config.
docker compose:

services:
  api_service:
        image: api_image
        environment:
          - API_ENDPOINT=http://service:3000/api/v1/
        ports:
          - "44399:44399"

app.config:
<add key="apiUrl" value="{{API_ENDPOINT}}" />
Its possible to get in .net core value of API_ENDPOINT from docker-compose?

Your api_image would need to be able to recognise the API_ENDPOINT variable and then know what to do with it - so unless your api_image has that function already, then you’ll need to create a custom image that does know what to do with the variable.

Any idea how i can do it in .net core? I tried Environment.GetEnvironmentVariable, but it still not working

ASP.NET core includes built-in support for environment variables as a configuration provider.
All the details are available here: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-3.1

Docker-compose simply sets the environment variables in the container, so this works just the same as using environment variables with dotnet core without using docker…

Now, I haven’t seen app.config used for dotnet core, so maybe that’s your problem, I thought it was a dotnet framework thing… Usually in dotnet core config is taken from appsettings.json, and overridden using environment variables.

From code you can use dependency injection to get access the values through IConfiguration:

public class MyClass
{
    private readonly IConfiguration _config:
    public MyClass(IConfiguration config) => _config = config;

    public SomeMethod()
    {
        var apiUrl = _config["API_ENDPOINT"];
    }
}