Docker variable expansion in compose entrypoint

2019-07-14 05:59发布

问题:

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.

回答1:

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\""