miken :
I'm trying to pass command line args to the main method of a Spring Boot app running in a Docker container. Running from the command line, I would do something like this:
$ mvn spring-boot:run -Dspring-boot.run.arguments=--greeting=Hello,--recipient=World
The app needs to pick these args up and do some processing with them:
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);
}
I have tried simply passing them in using the -e
flag:
docker run -p 8080:8080 -d -e "greeting=Hello" -e "recipient=World" test-image
My Dockerfile
looks like this:
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"]
Since options (args) are required, I keep receiving an error message and the app exits.
Evgeniy Strepetov :
You can provide all command line arguments just after name of your docker image in run
command.
Example:
docker run -p 8080:8080 test-image --recipient="World"--greeting="Hello"