I need to define the array of exposed ports for a given service from an environment variable.
If I define the ports array as below, it works without problems:
services:
open-connect:
ports: ["80:80","90:90"]
or
services:
open-connect:
ports: ['80:80','90:90']
However, if I define it in a .env file
EXPOSED_PORTS="'80:80','90:90'"
or
EXPOSED_PORTS='"80:80","90:90"'
services:
open-connect:
ports: [$EXPOSED_PORTS]
It gives me the error:
$ docker-compose up -d --build
ERROR: The Compose file './docker-compose.override.yml' is invalid because:
services.open-connect.ports contains an invalid type, it should be a number, or an object
It only works if the environment variable contains a single port mapping:
EXPOSED_PORTS="80:80"
How can I make this array of exposed ports configurable from an environment variable?
Update
The suggested similar question docker-compose variable substitution / interpolation with list, map, or array value is a bit old (2018) and doesn't provide a solution supported by docker-compose on its own; instead, it requires to use an external command to physically alter the docker-compose file with the array value.