Search code examples
javamacosdockerdocker-java

How to run a docker container from within java


I want to run the following command from within a Java Eclipse Project:

docker container run -p 4000:5000 virtuprint:vp

where virtuprint:vp is an image. I installed the docker-java api from here https://github.com/docker-java/docker-java using all its dependencies via maven. I am using MacOS, I guess that's causing my trouble.

I already read the docs and wikis, and just did some test code to see if it's running (and of course it's not.)

DockerClient dockerClient = DockerClientBuilder.getInstance().build();

        List<Container> containers = dockerClient.listContainersCmd().exec();

        for(Container c: containers) {
            System.out.println(c.getId());
        }

So I just want to print the container-ID in short term. Long term I want to run multiple docker containers on demand from within my java application. But if I run the code I get:

java.lang.NoClassDefFoundError: Could not initialize class org.newsclub.net.unix.NativeUnixSocket

I guess it's because he is trying to find unix sockets on places they aren't, but I neither know where they are nor how to tell him where to find them.


Solution

  • Played with the dependencies, downgraded to version 3.0.14 and sticked closely to this guide: https://www.baeldung.com/docker-java-api

    don't know why but now it works.