Docker variable expansion in compose entrypoint

十年热恋 提交于 2019-12-24 04:33:04

问题


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


来源:https://stackoverflow.com/questions/48184700/docker-variable-expansion-in-compose-entrypoint

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!