Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker Compose Nested Environment Variable

I have an existing app that uses a app config file that looks like:

"ConnectionInfo": {
    "ServerName": "The Server URL",
    "DatabaseName": "The DatabaseName",
    "UserName": "The User Name",
    "Password": "The Password"}

Now, when I have a simple setting, say

"ConnectionString":"My Connection String"

I understand how to override it in the compose.yml file:

environment:
  - ConnectionString=what I want it to be

The question is, how do you set, say, the server name in the top?

like image 839
Charles Gallo Avatar asked Aug 25 '17 20:08

Charles Gallo


People also ask

Can you use environment variables in Docker compose?

In Docker Compose, IT admins can use environment variables to generalize configurations for different situations, deployment environments and security contexts without editing the main project file(s) manually. Variables can either be passed as command-line arguments -- suitable for only a few parameters -- or via a .

How do I pass an environment variable in Docker run?

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.

Does Docker use .env file?

The .env file feature only works when you use the docker-compose up command and does not work with docker stack deploy . Both $VARIABLE and ${VARIABLE} syntax are supported.

Can I have 2 Docker compose files?

Using Multiple Docker Compose Files Use multiple Docker Compose files when you want to change your app for different environments (e.g., dev, staging, and production) or when you want to run admin tasks against a Compose application. This gives us one way to share common configurations.


2 Answers

Please use double underscore (__) as the following instead of a colon (:).

environment:
  - ConnectionInfo__ServerName=MyServerName

Please refer to Configuration in ASP.NET Core

For hierarchical config values specified in environment variables, a colon (:) may not work on all platforms. Double underscore (__) is supported by all platforms.

like image 96
idubnori Avatar answered Oct 06 '22 21:10

idubnori


You can set nested configurations using a colon to separate the nested sections:

To set the server name here:

"ConnectionInfo": {
    "ServerName": "override this via compose environment"
}

Override it like this:

environment:
  - ConnectionInfo:ServerName=MyServerName
like image 41
officer Avatar answered Oct 06 '22 21:10

officer