How to define an environment value witih : in name in docker compose?

Viewed 4538

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.

2 Answers

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>    

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
Related