How to handle a Docker image of a Jar with no main class?

775 views
Skip to first unread message

Frank Kelly

unread,
Aug 25, 2020, 4:45:30 PM8/25/20
to Jib users
I am creating a docker image that contains a Java library to be used as an `initContainer` to a Kubernetes Pod.

As a result my java library has no main class.
Is there anyway I can use jib for this? I'm pretty much a beginner at Jib and although I skimmed the docs it wasn't completely obvious if there was a way.

Thanks in advance,

-Frank

Chanseok Oh

unread,
Aug 25, 2020, 5:58:52 PM8/25/20
to Jib users
What are the exact contents that you want to put into your image and where? For example, let's say your project generates a Java library (an usual JAR artifact, say, my-lib-1.0.jar) using other dependency JARs (such as guava-*.jar or even another sub-module library inside your project). So, do you want to put them like

/some/dir/guava-...jar
/some/dir/commons-compress....jar
...
/some/dir/my-lib-1.0.jar

? Most likely this is not what Jib natively supports, but I'm trying to understand the exact requirements of yours.

Chanseok Oh

unread,
Aug 26, 2020, 10:56:50 AM8/26/20
to Jib users
I can certainly think of a few different hackish ways to achieve this (similar to some ideas here). However, your use case is not really what the Jib build plugin is designed for, so I don't really want to go into the details.

Rather, I think Jib CLI (a general-purpose CLI to create a container image) is the right tool in your case. However, we are actively working on revamping the Jib CLI interface, so you might want to wait a bit more for UI stability.
 


Frank Kelly

unread,
Aug 26, 2020, 11:00:27 AM8/26/20
to Chanseok Oh, Jib users
Thanks I was able to figure out a solution similar to the following

jib {

  allowInsecureRegistries = true

  from {

    //We don't need Java we need 'sh' to copy the file to the right location

    image ='docker://busybox'

  }

  to {

    image = imageName

    tags = [imageTag.toString()+"-"+System.currentTimeMillis(), 'latest']

  }

  container {

    creationTime = "USE_CURRENT_TIMESTAMP"

    //Just a dummy entrypoint

    entrypoint = ['/bin/sh','-c','sleep', '1']

  }

  extraDirectories {

    paths {

      path {

        // copies the contents of libs directory into / on the container

        from = file('build/libs')

      }

    }

  }

}


--
You received this message because you are subscribed to a topic in the Google Groups "Jib users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/jib-users/b1BHRzvU_Ek/unsubscribe.
To unsubscribe from this group and all its topics, send an email to jib-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jib-users/9796b747-50f5-4f81-8585-981a5978ed22n%40googlegroups.com.

Chanseok Oh

unread,
Aug 26, 2020, 11:08:38 AM8/26/20
to Jib users
Thanks for the update. The only concern (as also explained in the link in my previous post) is that Jib will probably duplicate your JAR artifact (and runtime dependencies). For example, I suspect your built image will end up having all of these:

/my-artifact-0.1.0-SNAPSHOT.jar (copied by "extraDirectories")
/app/classes/**/*.class (contents of my-artifact-0.1.0-SNAPSHOT.jar placed by Jib)
/app/resources/...
/app/libs/*.jar (runtime dependency JARs placed by Jib)

Check your image contents with a tool like dive. If you still want to use this hack, you'll probably want to filter out unnecessary files using the Layer-Filter extension (as explained here).

chunduru ganeshmanikanta

unread,
Apr 5, 2022, 2:23:03 AM4/5/22
to Jib users
Hello....

even I do have similar requirement where I need to build a jar using jib.

use case:
the jar I'm referring to is created in one module(lets say app1 project). I need to use the newly created jar from app1 project in another module(lets say app2 project), so I have placed jib related configuration in app2 project's pom file and built the app2 project... is this good approach to follow?

and I'm trying to build the image on to local docker daemon so, Im using the command mvn compile jib:dockerBuild. the build got success and images are created in my local docker. but Im not able validate whether the image is properly created for the jar or not!!

please help me out in validating the docker image(especially for the jar) 

Cap10Bill Nortman

unread,
Apr 14, 2023, 4:49:48 PM4/14/23
to Jib users
I've tried this but 
My War doesn't end up in the base image... it's in the target directory. It's packaged using other tools now jar for Jruby.

<plugin>
<artifactId>jib-maven-plugin</artifactId>
<groupId>com.google.cloud.tools</groupId>
<version>${jib.plugin.version}</version>
<configuration>
<workingDirectory>target</workingDirectory>
<container>
<appRoot>/usr/local/tomcat/webapps/ROOT</appRoot>
<ports>
<port>53000</port>
<port>5011</port>
<port>8080</port>
</ports>
</container>
<from>
<image>${jib.plugin.image.from}</image>
</from>
<to>
<image>${jib.plugin.image.to}</image>
<tags>
<tag>${jib.plugin.image.version}</tag>
</tags>
</to>
<extraFiles>
<extraFile>
<source>target/my-app.war</source>
<target>/usr/local/tomcat/webapps/ROOT/may-app.war</target>
</extraFile>
</extraFiles>
<level>
<parser>DEBUG</parser>
</level>
</configuration>
</plugin>


Command line to run it
docker run --rm -it
-p 8080:8080 -p 5011:5011 -p 53000:53000
-e CATALINA_BASE=/usr/local/tomcat
-e CATALINA_HOME=/usr/local/tomcat
-e CATALINA_OPTS="-Dcom.sun.management.jmxremote.port=53000
-Dcom.sun.management.jmxremote.ssl=false
-Dcom.sun.management.jmxremote.authenticate=false
-Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties
-Djavax.net.debug=ssl"
my-app-service bash

<jib.plugin.image.from>tomcat:8.5-jdk8-temurin-focal</jib.plugin.image.from>

Reply all
Reply to author
Forward
0 new messages