小编mik*_*ken的帖子

将命令行参数传递给在Docker中运行的Java应用程序(Spring Boot)

我正在尝试将命令行args传递给在Docker容器中运行的Spring Boot应用程序的main方法。从命令行运行,我将执行以下操作:

 $ mvn spring-boot:run -Dspring-boot.run.arguments=--greeting=Hello,--recipient=World
Run Code Online (Sandbox Code Playgroud)

该应用程序需要提取这些arg并对其进行一些处理:

public static void main(String[] args) {
    Options options = new Options();

    Option greeting = new Option("-g", "greeting", true, "Greeting");
    greeting.setRequired(true);
    greeting.addOption(greeting);

    Option recipient = new Option("-r", "recipient", true, "Recipient");
    recipient.setRequired(true); 
    recipient.addOption(recipient);

    try {
        cmd = parser.parse(options, args);
    } catch (ParseException e) {
        System.out.println(e.getMessage());
        formatter.printHelp("utility-name", options);
        System.exit(1);
    }

    // Do some processing using these args ...

    // Run the Spring Boot app
    SpringApplication.run(SpringBootApp.class, args);
}
Run Code Online (Sandbox Code Playgroud)

我试过简单地使用-e标志传递它们:

docker run …
Run Code Online (Sandbox Code Playgroud)

java command-line-arguments docker spring-boot dockerfile

4
推荐指数
2
解决办法
3134
查看次数