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.
If you want to pass multiple build arguments with docker build command you have to pass each argument with separate — build-arg. docker build -t <image-name>:<tag> --build-arg <key1>=<value1> --build-arg <key2>=<value2> .
This Dockerfile is very simple, but it is all you need to run a Spring Boot app with no frills: just Java and a JAR file. The build creates a spring user and a spring group to run the application. It is then copied (by the COPY command) the project JAR file into the container as app.
Passing Spring Profile in Docker run You can also pass spring profile as an environment variable while using docker run command using the -e flag. The option -e “SPRING_PROFILES_ACTIVE=dev” will inject the dev profile to the Docker container.
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"
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With