Pass command line args to Java app (Spring Boot) running in Docker
14,197
Solution 1
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"
Solution 2
You can try to modify your ENTRYPOINT
in your Dockerfile
like that:
ENTRYPOINT exec java -jar -Dspring-boot.run.arguments=--greeting=Hello,--recipient=World
Or you can pass argument at run:
docker run -p 8080:8080 test-image --recipient="World"--greeting="Hello"
Author by
miken
Updated on June 16, 2022Comments
-
miken almost 2 years
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.
-
miken over 5 yearsAwesome. I haven't seen anything resembling starting the container this way while trying to figure this out. Thanks @Evgeniy Strepetov
-
miken over 5 yearsI tried something similar with ENTRYPOINT but the syntax was a little funky, but this makes total sense. Thanks @veben
-
ddsultan over 3 yearsPlease note: passing system arguments only works if you have exec form entrypoint (e.g.:
["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
). Details are described here -
Paulo Merson about 3 yearsSetting fixed values for the arguments in the
ENTRYPOINT
line in the Dockerfile does not make sense to me.