Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passing docker environment variables to .net core

I've followed this article and the code on the github doesn't compile, tutorial is outdated I think. (Configuration = builder.Build();) throws error. So how can I access env passed from docker?


docker-compose

  myproj:
    image: mcr.microsoft.com/dotnet/core/sdk:2.2
    restart: on-failure
    working_dir: /MyProj
    command: bash -c "dotnet build MyProj.csproj && dotnet bin/Debug/netcoreapp2.2/MyProj.dll"
    ports:
      - 5001:5001
      - 5000:5000
    volumes:
      - "./MyProj:/MyProj"
    environment:
      DATABASE_HOST: database
      DATABASE_PASSWORD: Password

Startup.cs

public Service()
{
    Environment.GetEnvironmentVariable("DATABASE_PASSWORD"); // null
}

// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }

    app.Run(async (context) =>
    {
        context.Response.WriteAsync("Hello World!");
    });
}
like image 274
AppDeveloper Avatar asked Oct 10 '19 08:10

AppDeveloper


People also ask

How do I pass environment variables to Docker containers?

Using –env, -e When we launch our Docker container, we can pass environment variables as key-value pairs directly into the command line using the parameter –env (or its short form -e). As can be seen, the Docker container correctly interprets the variable VARIABLE1.

Is it possible to pass environment variables using Dockerfiles?

Passing Environment Variables Into a DockerfileDockerfile provides a dedicated variable type ENV to create an environment variable. We can access ENV values during the build, as well as once the container runs.

How to access environment variables in Docker run?

The standard approach to access environment variables in a.NET Core application is to use the static method public static string GetEnvironmentVariable (string variable); So in your case irrespective of what you pass either in the docker run command or through the launch settings file just use this method.

Can I use Docker env VARs in NET Core?

Using Docker env vars in .NET Core. One really good thing about packing your apps with Docker is that you're building once and deploying many times in many different places (environments), because of this it's a good idea to make your app configurable at the environment level (not just files).

How do I pass a variable to a docker container?

To pass environment variables to a container launched this way, you will have to configure the compose file to pass the session’s variables through to the Docker container. This configuration here passes the POSTGRES_USER variable to both the build environment and the runtime environment, and sets a default value if it does not exist.

How do I create a dockerfile in ASP NET Core?

Create a Dockerfile for an ASP.NET Core application Method 1: Create a Dockerfile in your project folder. Add the text below to your Dockerfile for either Linux or Windows Containers. The tags below are multi-arch meaning they pull either Windows or Linux containers depending on what mode is set in Docker Desktop for Windows.


3 Answers

The standard approach to access environment variables in a .NET Core application is to use the static method

public static string GetEnvironmentVariable (string variable);

So in your case irrespective of what you pass either in the docker run command or through the launch settings file just use this method . As an example for getting the Database password use this

string dbPassword = Environment.GetEnvironmentVariable("DATABASE_PASSWORD");

Additionally be sure to define the environment variables part of the dockerfile by adding the line

ENV DATABASE_PASSWORD some_default_value_or_can_be_empty
like image 162
Soumen Mukherjee Avatar answered Nov 03 '22 00:11

Soumen Mukherjee


You can pass env variable in build argument. E.g.

--build-arg ASPNETCORE_ENVIRONMENT=Development

Use below variable to set value:

ASPNETCORE_ENVIRONMENT

Code:

var environmentName = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");

You can use above variable in your code for environment name .

like image 23
Rahul Chavan Avatar answered Nov 02 '22 23:11

Rahul Chavan


You could read the values with IConfiguration which all done for you by default. If you have configured your host with defaults in Program.cs, then it will load the configuration in the following order

  1. appsetting.json
  2. appsettings..json
  3. secret.json
  4. environment variables
  5. cli args

For example, you could have a class

    public class DatabaseSettings
    {
        public string Host { get; set; }
        public string Password { get; set; }
    }

then in your Startup.cs you can read the values and bind them to a DatabaseSettings object.

var credentials = Configuration.GetSection("Database").Get<DatabaseSettings>();

Note: Your environment variables must have double underscores to separate between the key and the value section.

For example, if you were to pass this from appsettings.json or appsettings.Development.json file, this hierarchical JSON object

  "Database": {
    "Host": "address",
    "Password": "password" 
  }

Can be also represented when flattened like this

"Database:Host":"address",
"Database:Password":"Password"

Or when passed as environment variable can be done as

Database__Host=address
Database__Password=password

Remember, you need double underscore.

like image 42
Vergil C. Avatar answered Nov 02 '22 23:11

Vergil C.