7

I would like to pass an environment variable named ABC:DEF to a container defined in a docker-compose.yml.

How to do it?

If I write in a service definition

environment:
    ABC:DEF: ${ABC:DEF}

I got

ERROR: Invalid interpolation format for "environment" option in service: "${ABC:DEF}"

Edit: The goal of this question is to understand whether it is possible to use environment variables names which contain a : colon and how to properly define that in docker-compose.yml. A discussion what are the alternatives to using the : colon from the view of the "thing" running inside the affected container is not part of the question.

alik
  • 2,244
  • 3
  • 31
  • 44
  • 2
    A colon is not a valid character in an environment variable name in Linux. This is a Linux requirement, not docker. The colon is a special character used to modify how variables are expanded. – BMitch Oct 20 '18 at 10:06
  • Possible duplicate of [How can I escape a $ dollar sign in a docker compose file?](https://stackoverflow.com/questions/40619582/how-can-i-escape-a-dollar-sign-in-a-docker-compose-file) – kenorb Feb 16 '19 at 13:46

2 Answers2

8

Use the following line instead of ABC:DEF: ${ABC:DEF}:

environment:
  POSTGRES_PASSWORD: any-password 

[UPDATE]:

If : cannot be used in environment variables in your system, replace : with __ (double underscore).

Configuration_in _ASP.NET _Core_Reference

So I think that would be something like this:

ABC__DEF: <asp-environment>    
Benyamin Jafari
  • 27,880
  • 26
  • 135
  • 150
  • Thanks, but that does not help me to find a way how to properly define an environment variable containing a `:` colon, nor how to set it to a value of an environment variable containing a `:` colon. – alik Oct 20 '18 at 09:05
  • @alik, I understand your mean now, but really is there an environment with containing the colon? I tried `$ A:B=1` terminally and encountered with an error! – Benyamin Jafari Oct 20 '18 at 09:14
  • 1
    Yes, there is. For example, ASP.NET Core uses colons to express hierarchy. Environment variable `ABC:DEF=GHI` equals to `{ "ABC" { "DEF": "GHI" } }` if expressed in JSON. – alik Oct 20 '18 at 09:20
  • Just to clear things up, the container does not run ASP.NET Core. It's just an example that yes, there might be an environment containing the colon. – alik Oct 20 '18 at 09:40
  • 1
    I am curious how to properly transfer the value from outside into the container via docker-compose. I can change the variable names if there is no such a way or if it is not nice enough. – alik Oct 20 '18 at 09:42
  • @alik this [link](https://stackoverflow.com/a/48356770/3702377) is similar problem with nested variable (JSON), so I think your problem is there in right side of colon (`${ABC:DEF}`) – Benyamin Jafari Oct 20 '18 at 09:51
0

There are various ways to define environment variables in docker compose as given below

  1. Define it in compose file and pass the value via shell

enter image description here

    $ export TAG=v1.0

    $ docker-compose up -d
  1. You can set env variables while running docker-compose

    docker-compose run -e TAG=v2.0

  2. You can pass external variable file in compose file

enter image description here

  1. You can define the default values in .env file

enter image description here

When you set the same environment variable in multiple files, here’s the priority used by Compose to choose which value to use:

  1. Compose file
  2. Shell environment variables
  3. Environment file
  4. Dockerfile
  5. Variable is not defined
Manish R
  • 2,312
  • 17
  • 13
  • All in the docs https://docs.docker.com/compose/environment-variables/#the-env-file – Lawrence Cherone Oct 20 '18 at 09:00
  • Thanks, I've seen the docs. But I did not find a way how to properly define an environment variable containing a `:` colon, nor how to set it to a value of an environment variable containing a `:` colon. – alik Oct 20 '18 at 09:04