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!");
});
}
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.
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.
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.
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).
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.
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.
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
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 .
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With