Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker variable expansion in compose entrypoint

I've found that if I override an image's ENTRYPOINT in a docker-compose service entry, that it ignore variables defined in the environment section but will read them from an env file, e.g.

  someserver:
    image: "some-server:latest"
    restart: always
    ports:
      - "8849:8849"
    environment:
       javaMemoryLimit: 3056M
       JAVA_OPTS: "-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"
    entrypoint: java ${JAVA_OPTS} -Xmx${javaMemoryLimit} -jar /app.jar

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

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

But when I define the variable in an env file, e.g. .env

javaMemoryLimit=2098M
JAVA_OPTS=-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

Then variables are expanded as expected.

like image 560
KramKroc Avatar asked Mar 10 '26 00:03

KramKroc


1 Answers

Variables defined in the compose environment section are passed to the container, but not used by docker-compose itself to parse your yml file. The variables in the yml file are expanded with your host shell's environment (the shell where you run the docker-compose up command from) and/or the .env file contents.

Since you are running the entrypoint with the shell syntax, you can have the shell inside the container expand the variables instead of having docker-compose do it, by escaping the variables:

entrypoint: "java $${JAVA_OPTS} -Xmx$${javaMemoryLimit} -jar /app.jar"

You may need to add a /bin/sh to parse those variables:

entrypoint: "/bin/sh -c \"java $${JAVA_OPTS} -Xmx$${javaMemoryLimit} -jar /app.jar\""
like image 94
BMitch Avatar answered Mar 12 '26 15:03

BMitch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!