Java参数配置用于Docker

huangapple go评论102阅读模式
英文:

Java args config for Docker

问题

我正在使用Java创建Docker容器。但是jar包的参数配置应该如何进行呢?

我认为:

if (args.length == 2) new Listen().run(args[0], args[1]);
else System.out.println("Ex: docker run -it -e MQTT_HOST=localhost:1883 -e MQTT_TOPIC=test mqtt");
System.exit(1);

这样是否正确?

英文:

I'm doing docker container with java. But how should it be jar args config?

i think:

if (args.length == 2) new Listen().run(args[0],args[1]);
	else System.out.println("Ex: docker run -it -e MQTT_HOST=localhost:1883 -e MQTT_TOPIC=test mqtt");
	System.exit(1);

is it true?

答案1

得分: 1

你可以直接将Java参数传递给Docker运行,如下所示。docker run <image> java-args1 java-args2

docker run test-image:latest args1 args2

但是,我更倾向于使用System.getenv("ARGS1");方法来读取Docker环境变量,就像下面这样。

public static void main(String args[]) {
    String args1 = System.getenv("ARGS1");
    System.out.println("从Docker环境获取的参数:" + args1);
}

然后,在Docker中传递环境变量。

docker run -e ARGS1=testing-args test-image:latest
英文:

You can pass java args directly to docker run like. docker run &lt;image&gt; java-args1 java-args2

    docker run test-image:latest args1 args2

But, I would prefer to use System.getenv(&quot;ARGS1&quot;); method to read docker environment variables, like below.

public static void main(String args[]) {
	String args1 = System.getenv(&quot;ARGS1&quot;);
	System.out.println(&quot;Arguments from docker env : &quot; + args1);
}

And, pass environment variable in docker..

      docker run -e ARGS1=testing-args test-image:latest

huangapple
  • 本文由 发表于 2020年8月25日 14:30:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/63573174.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定