'integer variable expression in docker-compose.yml

I would like to use integer expression in docker-compose.yml to set ports based on the BASE_PORT variable.

I have a .env file with the following content:

BASE_PORT=57900

My start bash script looks like this:

#/bin/sh

. .env
docker-compose stop
docker-compose rm -v -f
docker-compose up -d adminserver

docker-compose.yml

...
services:
  adminserver:
    image: ....
    hostname: ...
    ports:
      - ${BASE_PORT}:8001
    ...

This configuration works like a charm, docker ps | grep 8001 returns with a proper content.

But I would like to use more port configurations and more ports sections:

...
services:
  adminserver:
    image: ....
    hostname: ...
    ports:
      - $((BASE_PORT+1)):8001
      - $((BASE_PORT+2)):8002
      - $((BASE_PORT+3)):8003
    ...

  databaseserver:
    image: ....
    hostname: ...
    ports:
      - $((BASE_PORT+10)):1541

  zookeeper:
    image: ....
    hostname: ...
    ports:
      - $((BASE_PORT+30)):2181

  kafka:
    image: ....
    hostname: ...
    ports:
      - $((BASE_PORT+40)):9092
      - $((BASE_PORT+41)):9093
      - $((BASE_PORT+42)):9094

The $((BASE_PORT+1)) expression works in shell but it does not work in my *.yml file.

I get this error back:

ERROR: Invalid interpolation format for "ports" option in service "adminserver": "$((BASE_PORT+1)):8001"

Any idea how to make it works?



Solution 1:[1]

if u want '10000-10003:8000-8003'

try set:

BASE_PORT_PREFIX=1000

then:

ports:
      - ${BASE_PORT_PREFIX}0:8000
      - ${BASE_PORT_PREFIX}1:8001
      - ${BASE_PORT_PREFIX}2:8002
      - ${BASE_PORT_PREFIX}3:8003

this works not only in port-range, but everywhere besides .env, yaml...

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 fanlix