将命令行参数传递给 Java 应用程序(Spring 启动)运行 在 Docker
Pass command line args to Java app (Spring Boot) running in Docker
我正在尝试将命令行参数传递给 Spring Boot 应用程序 运行 的主要方法 Docker容器。 运行 从命令行,我会做这样的事情:
$ mvn spring-boot:run -Dspring-boot.run.arguments=--greeting=Hello,--recipient=World
应用程序需要选取这些参数并对它们进行一些处理:
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);
}
我试过使用 -e
标志简单地传递它们:
docker run -p 8080:8080 -d -e "greeting=Hello" -e "recipient=World" test-image
我的 Dockerfile
看起来像这样:
FROM openjdk:8-jdk-alpine
VOLUME /tmp
COPY ./target/spring-boot-app-0.0.1-SNAPSHOT.jar app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
由于需要选项 (args),我不断收到错误消息并退出应用程序。
您可以在 run
命令中的 docker 图像名称后提供所有命令行参数。
示例:
docker run -p 8080:8080 test-image --recipient="World"--greeting="Hello"
您可以尝试像这样修改 Dockerfile
中的 ENTRYPOINT
:
ENTRYPOINT exec java -jar -Dspring-boot.run.arguments=--greeting=Hello,--recipient=World
或者您可以在 运行:
传递参数
docker run -p 8080:8080 test-image --recipient="World"--greeting="Hello"
我正在尝试将命令行参数传递给 Spring Boot 应用程序 运行 的主要方法 Docker容器。 运行 从命令行,我会做这样的事情:
$ mvn spring-boot:run -Dspring-boot.run.arguments=--greeting=Hello,--recipient=World
应用程序需要选取这些参数并对它们进行一些处理:
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);
}
我试过使用 -e
标志简单地传递它们:
docker run -p 8080:8080 -d -e "greeting=Hello" -e "recipient=World" test-image
我的 Dockerfile
看起来像这样:
FROM openjdk:8-jdk-alpine
VOLUME /tmp
COPY ./target/spring-boot-app-0.0.1-SNAPSHOT.jar app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
由于需要选项 (args),我不断收到错误消息并退出应用程序。
您可以在 run
命令中的 docker 图像名称后提供所有命令行参数。
示例:
docker run -p 8080:8080 test-image --recipient="World"--greeting="Hello"
您可以尝试像这样修改 Dockerfile
中的 ENTRYPOINT
:
ENTRYPOINT exec java -jar -Dspring-boot.run.arguments=--greeting=Hello,--recipient=World
或者您可以在 运行:
传递参数docker run -p 8080:8080 test-image --recipient="World"--greeting="Hello"