Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker variable expansion in compose environment

I've found that if I define an environment variable in a docker-compose service entry, that it will not be expanded in other variables defined in the environment section but it will be expanded if the variable is defined in an env file, e.g.

  someserver:
    image: "some-server:latest"
    restart: always
    ports:
      - "8849:8849"
    environment:
       javaMemoryLimit: 3056M
       JAVA_OPTS: "-Xmx${javaMemoryLimit} -Dcom.sun.management.jmxremote=true -Dcom.sun.management.jmxremote.port=8849 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.local.only=false -Dcom.sun.management.jmxremote.rmi.port=8849 -Djava.rmi.server.hostname=localhost"

When I do a docker-compose up with this I get warnings about variable not being set:

WARNING: The javaMemoryLimit variable is not set. Defaulting to a blank string.

The same occurs if I use the list form of environment definition.

But if the variable javaMemoryLimit is defined in a .env then the expansion is fine. I've also tried using $$javaMemoryLimit and then I don't get the warning message but the variable is not expanded when the container actually starts. Any ideas?

like image 905
KramKroc Avatar asked Oct 19 '25 05:10

KramKroc


1 Answers

This is deliberate variable substitution behavior. Only .env and variables from the shell environment are evaluated at the docker-compose.yaml level. Otherwise, ${javaMemoryLimit} will expand to an empty string.

As a side note: even more annoying is the fact that NO variable substitution whatsoever is done in files specified byenv_file:.

That said, if you do "-Xmx$${javaMemoryLimit} -D... whatever expands JAVA_OPTS might work since that should expand to -Xmx${javaMemoryLimit} -D... (instead of -Xmx -D...) in your ENTRYPOINT.

like image 178
nyet Avatar answered Oct 21 '25 13:10

nyet