Ok, I encountered linking problems while creating jib docker image. I copy the files i want into container
jib {
allowInsecureRegistries = true
extraDirectories{
paths{
path{
from = file('jnetpcap/jib')
into = '/native'
}
}
}
.
.
.
and in other task, i point to those libraries
task cmdScript(type: CreateStartScripts) {
mainClassName = "cic.cs.unb.ca.ifm.Cmd"
applicationName = "cfm"
outputDir = new File(project.buildDir, 'scripts')
classpath = jar.outputs.files + project.configurations.runtime
defaultJvmOpts = ["-Djava.library.path=/native"]
}
I checked, and those libraries are added correctly to container. It is not a problem with copying libs, but setting up linker.
cmdScript sets correct linker if i build project with distTar, but i don't know how to set up linker when building it with jibDockerBuild. I couldn't find anwer to my issue here so decided to seek help on SO.
UPDATE
I have found some clues here. I have updated my jib task by adding
jib {
allowInsecureRegistries = true
extraDirectories{
paths{
path{
from = file('jnetpcap/jib')
into = '/native'
}
}
}
container.jvmFlags = ["-Djava.library.path=/native/*"]
But I keep getting the same error.
error message is
exception in thread main java.lang.unsatisfiedlinkerror 'long com.slytechs.library.NativeLibrary.dlopen(java.lang.String)'
The issue is largely unrelated to Jib. The root cause is missing required libraries inside the container environment.
First things first, it should be
container.jvmFlags = ["-Djava.library.path=/native"]
(not/native/*
with the asterisk).Now, jNetPcap is a Java wrapper around Libpcap and WinPcap libraries found on various Unix and Windows platforms. That is, on Linux (which is the OS of the container you are building), it depends on Libpcap and requires it to be installed on the system. Most OpenJDK container images (including the one Jib uses as a base image) does not come with Libpcap pre-installed, and I suspect the first problem being that you are not installing Libpcap into the container.
jNetPcap also requires loading other native libraries. In the case of my example below, they were the two
.so
shared object files that come with the jNetPcap package:libjnetpcap-pcap100.so
andlibjnetpcap.so
.For explanation, below is the complete example that creates a working container image.
Dockerfile
MyMain.java
Therefore, as long as you copy necessary dependent libraries and have correct configuration, you should be able to make it work with Jib.
For installing Libpcap, I can think of a couple options:
apt-get install libpcap-dev
as above) and configurejib.from.image
to use it.libpcap.so
file into, say,/usr/lib
, using theextraDirectories
feature. (You can even make your Gradle project dynamically download the file when building your project.)For copying jNetPcap native libraries (
libjnetpcap-pcap100.so
andlibjnetpcap.so
), it's the same story. However, looks like you already manually downloaded and attempted copying them using theextraDirectories
feature, so I guess you can continue doing so. But still, preparing a custom base image is another viable option. Note that in the example above, I configured-Djava.library.path=...
for jNetPcap (BTW, there are many other ways to have Linux and JVM load shared libraries in an arbitrary directory), but if you copy the.so
files into some standard locations (for example,/usr/lib
), you wouldn't even need to set-Djava.library.path
.For all of the native libraries (
.so
files) above, make sure to download the right binaries compatible with the container architecture and OS (probably amd64 and Linux in your case).