I try to run Pi4J Library inside a docker container on RaspberryPi Host. When the app is running on RaspberryPi Host, everything is working fine. But when its run inside a docker container, the Pi4J native library cant be loaded.
pom
<properties>
<!-- DEPENDENCIES VERSIONS -->
<pi4j.version>2.1.1</pi4j.version>
</properties>
<dependencies>
<!-- include Pi4J Core -->
<dependency>
<groupId>com.pi4j</groupId>
<artifactId>pi4j-core</artifactId>
<version>${pi4j.version}</version>
</dependency>
<!-- include Pi4J Plugins (Platforms and I/O Providers) -->
<dependency>
<groupId>com.pi4j</groupId>
<artifactId>pi4j-plugin-raspberrypi</artifactId>
<version>${pi4j.version}</version>
</dependency>
<dependency>
<groupId>com.pi4j</groupId>
<artifactId>pi4j-plugin-pigpio</artifactId>
<version>${pi4j.version}</version>
</dependency>
</dependencies>
Dockerfile
FROM eclipse-temurin:17-jdk as builder
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} application.jar
RUN java -Djarmode=layertools -jar application.jar extract
FROM eclipse-temurin:17-jre-focal
COPY --from=builder dependencies/ ./
COPY --from=builder snapshot-dependencies/ ./
COPY --from=builder spring-boot-loader/ ./
COPY --from=builder application/ ./
ENTRYPOINT ["java", "org.springframework.boot.loader.JarLauncher"]
When i run the app without setting the pi4j.library.path
im getting this error.
java.lang.UnsatisfiedLinkError: /tmp/libpi4j-pigpio3285308132227026624.so: libpigpio.so.1:
cannot open shared object file: No such file or directory
...
Caused by: java.lang.UnsatisfiedLinkError: Pi4J was unable to extract and load the native library
[/lib/aarch64/libpi4j-pigpio.so] from the embedded resources inside this JAR
[/application/BOOT-INF/lib/pi4j-library-pigpio-2.1.1.jar]. to a temporary location on this
system. You can alternatively define the 'pi4j.library.path' system property to override this
behavior and specify the library path.
/tmp folder has permission 777
When im setting the pi4j.library.path
and add the volume to the docker container.
Caused by: java.lang.UnsatisfiedLinkError: /app/data/pi4j/libpi4j-pigpio.so: libpigpio.so.1:
cannot open shared object file: No such file or directory
If anyone got experience with Pi4j and Docker, please help me - THANKS :)
UPDATE 1 - 2022-09-01
Problem seems to be, that java cant load native libraries inside the docker container with System.load()
function.
Host
ldd libpi4j-pigpio.so
linux-vdso.so.1 (0x0000007f8460d000)
libpigpio.so.1 => /lib/libpigpio.so.1 (0x0000007f844ca000)
libc.so.6 => /lib/aarch64-linux-gnu/libc.so.6 (0x0000007f84358000)
/lib/ld-linux-aarch64.so.1 (0x0000007f845df000)
libpthread.so.0 => /lib/aarch64-linux-gnu/libpthread.so.0 (0x0000007f843
inside Docker
ldd libpi4j-pigpio.so
linux-vdso.so.1 (0x0000007fb06ac000)
libpigpio.so.1 => not found
libc.so.6 => /lib/aarch64-linux-gnu/libc.so.6 (0x0000007fb04f0000)
/lib/ld-linux-aarch64.so.1 (0x0000007fb067c000)
UPDATE 2 - 2022-09-01
The Problem is quite easy, the library libpigpio.so.1 is missing inside the container. Currently i do not know how to install it. For now my workaround/solution is to mount the missing lib to the docker. container.
volumes:
- type: bind
source: /lib/libpigpio.so.1
target: /lib/libpigpio.so.1
read_only: true