Run a docker image built by Jib with multiple main class and choose which main class to execute at Runtime

45 views Asked by At

I have a java gradle project with multiple main class, i want to build a docker image with Jib and chose which main class to run when i execute docker run.

Is it possible to do this with Jib or i have to build multiple docker image for each main class i have ?

If i dont choose a mainclass in jib gradle config, it failed with :

Multiple valid main classes were found: ...

I try with a custom entrypoint, but no success for now, but i dont no how to pass the MainClass as an argument

container {
   entrypoint = ['/bin/sh', '-c', 'java -cp $( cat /app/jib-classpath-file ) MainClass']
}
2

There are 2 answers

1
Tom On

I finally found a solution quite simple with jib :

container {
    entrypoint = ['/bin/sh', '-c', 'java -cp $( cat /app/jib-classpath-file ) $MAINCLASS']
}

and

docker run -it -e MAINCLASS=HelloWorldJob image:1.0.0
0
Chanseok Oh On

Container runtimes (e.g., Docker, Kubernetes, etc.) provide a way to override the entrypoint set in an image to whatever value you want. For example, with the Docker engine, you could do

$ docker run -it --entrypoint java image:1.0.0 -cp /app/resources:/app/classes:/app/libs/* example.HelloJarWorld

If i dont choose a mainclass in jib gradle config, it failed with :

Multiple valid main classes were found: ...

If you problem is the failure above due to multiple main classes, you can set container.mainClass to your desired main class.

Property Type Default Description
mainClass String Inferred** The main class to launch your application from. The value can also be initialized lazily with a provider.