撰写入口点中的Docker变量扩展

撰写入口点中的Docker变量扩展

本文介绍了撰写入口点中的Docker变量扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现,如果我在docker-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

当我执行 docker-compose up 时,我会收到有关未设置变量的警告:

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.

但是当我在环境文件中定义变量时,例如.env

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.

推荐答案

在组成环境部分中定义的变量被传递到容器,但 docker-compose 本身不用于解析yml文件。 yml文件中的变量随主机外壳环境(运行 docker-compose up 命令的外壳)和/或 .env 文件内容。

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.

由于使用shell语法运行入口点,因此可以使容器内部的shell扩展变量通过转义变量来完成 docker-compose 的操作:

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"

您可能需要添加/ bin / sh来解析这些变量:

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

entrypoint: "/bin/sh -c \"java $${JAVA_OPTS} -Xmx$${javaMemoryLimit} -jar /app.jar\""

这篇关于撰写入口点中的Docker变量扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 19:59