19

I am trying to get an application that works perfectly on my machine to run on docker, here is my docker file :

FROM openjdk:11-jre-slim
VOLUME /tmp
ADD someJar.jar someJar.jar
ADD lib lib
ADD config.properties config.properties
ENTRYPOINT ["java", "-javaagent:lib/aspectjweaver-1.9.2.jar", 
    "-javaagent:lib/spring-instrument-5.1.6.RELEASE.jar", "--module-path", 
    "lib/javafx-sdk-11.0.2", "--add-modules=javafx.controls", "- 
    Dprism.verbose=true", "-jar","someJar.jar"]

I also tried to base it off of the alpine openjdk11 release with the same result :

FROM adoptopenjdk/openjdk11:alpine
VOLUME /tmp
RUN apk update && apk add libx11 mesa-gl gtk+3.0 && apk update
ADD someJar.jar someJar.jar
ADD lib lib
ADD config.properties config.properties
ENTRYPOINT ["java", "-javaagent:lib/aspectjweaver-1.9.2.jar", "-javaagent:lib/spring-instrument-5.1.6.RELEASE.jar", "--module-path", "lib", "--add-modules=javafx.controls", "-Dprism.verbose=true", "-jar","someJar.jar"]

The lib folder contains the linux flavor of the openJFX runtime (.so files and .jar files). I am developing this on a Windows machine with the Windows-equivalent of the openJDK runtime and it works perfectly. When running the container I get the following output :

Prism pipeline init order: es2 sw
Using Double Precision Marlin Rasterizer
Using dirty region optimizations
Not using texture mask for primitives
Not forcing power of 2 sizes for textures
Using hardware CLAMP_TO_ZERO mode
Opting in for HiDPI pixel scaling
Prism pipeline name = com.sun.prism.es2.ES2Pipeline
Loading ES2 native library ... prism_es2
GraphicsPipeline.createPipeline failed for com.sun.prism.es2.ES2Pipeline
java.lang.UnsatisfiedLinkError: no prism_es2 in java.library.path: [/usr/java/packages/lib, /usr/lib/x86_64-linux-gnu/jni, /lib/x86_64-linux-gnu, /usr/lib/x86_64-linux-gnu, /usr/lib/jni, /lib, /usr/lib]
    at java.base/java.lang.ClassLoader.loadLibrary(ClassLoader.java:2660)
    at java.base/java.lang.Runtime.loadLibrary0(Runtime.java:829)
    at java.base/java.lang.System.loadLibrary(System.java:1867)
    at javafx.graphics/com.sun.glass.utils.NativeLibLoader.loadLibraryInternal(NativeLibLoader.java:150)
    at javafx.graphics/com.sun.glass.utils.NativeLibLoader.loadLibrary(NativeLibLoader.java:52)
    at javafx.graphics/com.sun.prism.es2.ES2Pipeline.lambda$static$0(ES2Pipeline.java:68)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at javafx.graphics/com.sun.prism.es2.ES2Pipeline.<clinit>(ES2Pipeline.java:50)
    at java.base/java.lang.Class.forName0(Native Method)
    at java.base/java.lang.Class.forName(Class.java:315)
    at javafx.graphics/com.sun.prism.GraphicsPipeline.createPipeline(GraphicsPipeline.java:187)
    at javafx.graphics/com.sun.javafx.tk.quantum.QuantumRenderer$PipelineRunnable.init(QuantumRenderer.java:91)
    at javafx.graphics/com.sun.javafx.tk.quantum.QuantumRenderer$PipelineRunnable.run(QuantumRenderer.java:124)
    at java.base/java.lang.Thread.run(Thread.java:834)
*** Fallback to Prism SW pipeline
Prism pipeline name = com.sun.prism.sw.SWPipeline
(X) Got class = class com.sun.prism.sw.SWPipeline
Initialized prism pipeline: com.sun.prism.sw.SWPipeline
Exception in thread "main" java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:47)
    at org.springframework.boot.loader.Launcher.launch(Launcher.java:86)
    at org.springframework.boot.loader.Launcher.launch(Launcher.java:50)
    at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51)
Caused by: java.lang.UnsupportedOperationException: Unable to open DISPLAY
    at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication.lambda$new$6(GtkApplication.java:173)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication.<init>(GtkApplication.java:171)
    at javafx.graphics/com.sun.glass.ui.gtk.GtkPlatformFactory.createApplication(GtkPlatformFactory.java:41)
    at javafx.graphics/com.sun.glass.ui.Application.run(Application.java:144)
    at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.startup(QuantumToolkit.java:258)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.startup(PlatformImpl.java:269)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.startup(PlatformImpl.java:158)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.startToolkit(LauncherImpl.java:658)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:678)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
    at java.base/java.lang.Thread.run(Thread.java:834)

Any idea how i can get a Java app based on OpenJFX11 to work in Docker?

******UPDATE******

I made some progress on this. I found that putting my openJFX runtime files DIRECTLY in my lib folder made it progress a bit more. The new error became this:

ImportError: libGL.so.1: cannot open shared object file: No such file or directory

After a bit of research, i added this RUN command in my dockerfile :

RUN apt-get update && apt-get install libgl1-mesa-glx -y

This gives me a new stacktrace :

Prism pipeline init order: es2 sw
Using Double Precision Marlin Rasterizer
Using dirty region optimizations
Not using texture mask for primitives
Not forcing power of 2 sizes for textures
Using hardware CLAMP_TO_ZERO mode
Opting in for HiDPI pixel scaling
Prism pipeline name = com.sun.prism.es2.ES2Pipeline
Loading ES2 native library ... prism_es2
    succeeded.
GLFactory using com.sun.prism.es2.X11GLFactory
(X) Got class = class com.sun.prism.es2.ES2Pipeline
GraphicsPipeline.createPipeline: error initializing pipeline 
com.sun.prism.es2.ES2Pipeline
*** Fallback to Prism SW pipeline
Prism pipeline name = com.sun.prism.sw.SWPipeline
(X) Got class = class com.sun.prism.sw.SWPipeline
Initialized prism pipeline: com.sun.prism.sw.SWPipeline
Exception in thread "main" java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:47)
    at org.springframework.boot.loader.Launcher.launch(Launcher.java:86)
    at org.springframework.boot.loader.Launcher.launch(Launcher.java:50)
    at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51)
Caused by: java.lang.UnsupportedOperationException: Unable to open DISPLAY
    at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication.lambda$new$6(GtkApplication.java:173)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication.<init>(GtkApplication.java:171)
    at javafx.graphics/com.sun.glass.ui.gtk.GtkPlatformFactory.createApplication(GtkPlatformFactory.java:41)
    at javafx.graphics/com.sun.glass.ui.Application.run(Application.java:144)
    at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.startup(QuantumToolkit.java:258)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.startup(PlatformImpl.java:269)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.startup(PlatformImpl.java:158)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.startToolkit(LauncherImpl.java:658)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:678)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
    at java.base/java.lang.Thread.run(Thread.java:834)

******ANOTHER UPDATE******

Digging through the JavaFX code in the GtkApplication.class file, this is the section that is failing :

int libraryToLoad = _queryLibrary(gtkVersion, gtkVersionVerbose);

AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
        if (libraryToLoad == QUERY_NO_DISPLAY) {
            throw new UnsupportedOperationException("Unable to open DISPLAY");
        } else if (libraryToLoad == QUERY_USE_CURRENT) {
            if (gtkVersionVerbose) {
                System.out.println("Glass GTK library to load is already loaded");
            }
        } else if (libraryToLoad == QUERY_LOAD_GTK2) {
            if (gtkVersionVerbose) {
                System.out.println("Glass GTK library to load is glassgtk2");
            }
            NativeLibLoader.loadLibrary("glassgtk2");
        } else if (libraryToLoad == QUERY_LOAD_GTK3) {
            if (gtkVersionVerbose) {
                System.out.println("Glass GTK library to load is glassgtk3");
            }
            NativeLibLoader.loadLibrary("glassgtk3");
        } else {
            throw new UnsupportedOperationException("Internal Error");
        }
        return null;
    });

Do i have a missing library or something?

... HELP?

Thanks

Martin
  • 1,977
  • 5
  • 30
  • 67
  • read everything i could find on this error, tried adding -e DISPLAY=host.docker.internal:0 to the docker run command and still getting the same error. – Martin Jul 11 '19 at 19:40
  • it seems an error with the load of javaFX, maybe you have to investigate better on this – emish89 Jul 12 '19 at 08:37
  • You're trying to run a Java application with GUI within a Linux Container. To my knowledge, the container runs headless and thus can't show a user interface. In your comment, you try to provide the current display to docker to be used, but I honestly doubt this works across the Linux/Windows boundary. I'm happy to be proven wrong, but I don't think it's possible with Docker to display a Linux GUI on a Windows host. – cello Jul 21 '19 at 20:28
  • @cello I feel like you could probably make this work by using an XServer. – flakes Jul 21 '19 at 21:14
  • 1
    Possible duplicate of [Can you run GUI applications in a Docker container?](https://stackoverflow.com/questions/16296753/can-you-run-gui-applications-in-a-docker-container/16311264) – flakes Jul 21 '19 at 21:22
  • The application has a property i can set to run with no GUI whatsoever, which is enabled when running in a container. My code does use Task and other JavaFX classes but I am not expecting any GUI to appear in this case, it runs completely silently in the background. – Martin Jul 22 '19 at 11:17
  • unfortunately no, same behavior of the ES2PipeLine failing for seemingly no reason and the SWPipeLine failing because of the "unable to open DISPLAY" error. – Martin Jul 22 '19 at 12:04
  • as my post says, the lib folder contains the openjfx runtime files and they are definitely being recognized. – Martin Jul 22 '19 at 12:11

1 Answers1

13

I finally got this to work! I had to install VcXsrv on my Windows host and add this to the docker run command :

-e DISPLAY=MY IP ADDRESS:0.0

My app now starts fine and outputs the following before actually starting the Spring initialization :

Prism pipeline init order: es2 sw
Using Double Precision Marlin Rasterizer
Using dirty region optimizations
Not using texture mask for primitives
Not forcing power of 2 sizes for textures
Using hardware CLAMP_TO_ZERO mode
Opting in for HiDPI pixel scaling
Prism pipeline name = com.sun.prism.es2.ES2Pipeline
Loading ES2 native library ... prism_es2
    succeeded.
GLFactory using com.sun.prism.es2.X11GLFactory
MESA-LOADER: failed to open swrast (search paths /usr/lib/xorg/modules/dri)
libGL error: failed to load driver: swrast
Prism-ES2 Error : GL_VERSION (major.minor) = 1.4
(X) Got class = class com.sun.prism.es2.ES2Pipeline
GraphicsPipeline.createPipeline: error initializing pipeline com.sun.prism.es2.ES2Pipeline
*** Fallback to Prism SW pipeline
Prism pipeline name = com.sun.prism.sw.SWPipeline
(X) Got class = class com.sun.prism.sw.SWPipeline
Initialized prism pipeline: com.sun.prism.sw.SWPipeline
MESA-LOADER: failed to open swrast (search paths /usr/lib/xorg/modules/dri)
libGL error: failed to load driver: swrast
vsync: true vpipe: false 

There are still some errors that i will try and work through but it actually starts now!

Full dockerfile as requested, I am still working through the errors that are causing it to fallback to the prism pipeline, will update later :

FROM adoptopenjdk/openjdk11:alpine
VOLUME /tmp
RUN apk update && apk add libx11 mesa-gl gtk+3.0 mesa-dri-swrast mesa-demos 
    && apk update
ADD someJar.jar someJar.jar
ADD lib lib
ADD config.properties config.properties
ENTRYPOINT ["java", "-javaagent:lib/aspectjweaver-1.9.2.jar", "-javaagent:lib/spring-instrument-5.1.6.RELEASE.jar", "--module-path", "lib", "--add-modules=javafx.controls", "-Dprism.verbose=true", "-Djava.awt.headless=true", "-jar","someJar.jar"]

******FINAL SETUP******

I dug into the libGL errors and the hassle of getting the ES2 pipeline to work is not even worth it for my needs. It would involve messing with Nvidia and CUDA drivers and is totally useless since my app is just a background service anyway. Here is the final setup to make everything work :

Dockerfile (switched back to openjdk for consistency with my other app and figured out the bare minimum packages to install)

FROM openjdk:11-jre-slim
RUN apt-get update && apt-get install libgtk-3-0 libglu1-mesa -y && apt-get update
VOLUME /tmp
ADD someJar.jar someJar.jar
ADD lib lib
ADD config.properties config.properties
ENTRYPOINT ["java", "-javaagent:lib/aspectjweaver-1.9.2.jar", "-javaagent:lib/spring-instrument-5.1.6.RELEASE.jar", "--module-path", "lib/javafx-sdk-11.0.2", "-jar", "someJar.jar"]

Docker build command

docker build -f Dockerfile -t some_service .

Docker run command

docker run -t --name Some_Service -e DISPLAY=MY IP:0.0 -e SOME_VARIABLE= --link mySQLSRV:mysql some_service

Hope this helps someone, this took me days to get working!

Martin
  • 1,977
  • 5
  • 30
  • 67
  • Nice trick, it would be fine to have the full DockerFile though – pdem Jul 22 '19 at 14:30
  • 1
    added full dockerfile – Martin Jul 22 '19 at 14:36
  • Unfortunately this only works for a few minutes then out of nowhere i get this error : 32mMessage: java: Fatal IO error 11 (Resource temporarily unavailable) on X server xxx.xxx.xxx.xxx:0.0. Any idea?! – Martin Sep 04 '19 at 15:17
  • Seems related (32/64 bit mixing in libs) https://stackoverflow.com/questions/25790890/xio-fatal-io-error-11 you may try on a linux host or vm – pdem Sep 04 '19 at 15:59
  • sadly i still get the DISPLAY error $ docker run -i -t apps:latest -e DISPLAY=x.my.ip.x:0.0 Exception in thread "main" java.lang.UnsupportedOperationException: Unable to open DISPLAY at com.sun.glass.ui.gtk.GtkApplication.lambda$new$6(GtkApplication.java:173) .... the DISPLAY env var didn't seem to fix my problem – Anand Dec 19 '19 at 10:37
  • See this post for the final answer! https://stackoverflow.com/questions/59076546/cannot-run-javafx-app-on-docker-for-more-than-a-few-minutes – Martin Feb 10 '20 at 19:57