Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Problem running Docker with com.github.dockerjava

I am trying to use docker and run my image with this Java Library: com.github.dockerjava

Here is the code :

import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.command.CreateContainerCmd;
import com.github.dockerjava.api.command.PullImageResultCallback;
import com.github.dockerjava.api.exception.NotFoundException;
import com.github.dockerjava.api.model.Container;
import com.github.dockerjava.api.model.ContainerNetwork;
import com.github.dockerjava.api.model.Image;
import com.github.dockerjava.core.DockerClientBuilder;

public static void main(String[] args) {
        String containerName = "mycontainer_name";
        String dockerImage = "myimage";

        // Get the Docker client
        System.out.println("Get Docker client");
        DockerClient dockerClient = DockerClientBuilder.getInstance().build();

        // Check if the container is already running
        if (dockerContainerExists(containerName, dockerClient)) {
            System.out.println("Container already exists");
            dockerStop(containerName, dockerClient);
            DockerRm(containerName, dockerClient);
        }

        CreateContainerCmd createContainer = dockerClient
                .createContainerCmd(dockerImage).withName(containerName);
        createContainer.withTty(true);
        createContainer.exec();

        dockerClient.killContainerCmd(containerName).exec();
        dockerClient.removeContainerCmd(containerName).exec();

        // runDataGenerator(new Pcap(), args);
    }

Here is my pom.xml dependency :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<dependency>
    <groupId>com.github.docker-java</groupId>
    <artifactId>docker-java</artifactId>
    <version>3.3.0</version>
</dependency>

But I get this error :

Exception in thread "main" java.lang.IllegalArgumentException: Unsupported protocol scheme: npipe:////./pipe/docker_engine
        at com.github.dockerjava.jaxrs.JerseyDockerHttpClient.<init>(JerseyDockerHttpClient.java:225)
        at com.github.dockerjava.jaxrs.JerseyDockerHttpClient.<init>(JerseyDockerHttpClient.java:49)
        at com.github.dockerjava.jaxrs.JerseyDockerHttpClient$Builder.build(JerseyDockerHttpClient.java:124)
        at com.github.dockerjava.core.DockerClientBuilder.build(DockerClientBuilder.java:106)
        at org.package.main(Pcap.java:1108)

I am on Windows and have Docker Desktop running (by the way it works perfectly on Linux) and I saw the same type of error for other people but I couldn’t find any answers.
If someone has the answer to this or has faced the same problem I am up for answers…

>Solution :

The error message "Unsupported protocol scheme: npipe:////./pipe/docker_engine" indicates that the Docker Java API is trying to connect to Docker using the "npipe" protocol, which is specific to Windows. This is likely because you are running the code on a Windows machine.

To fix this issue, you need to configure the Docker Java API to use the correct protocol for your operating system. You can do this by setting the DOCKER_HOST environment variable to the appropriate value:

For Windows, set DOCKER_HOST to "tcp://localhost:2375". This tells the Docker Java API to use the TCP protocol to connect to Docker.
For Linux or macOS, you can leave DOCKER_HOST unset, which will cause the Docker Java API to use the default Unix socket.
Here’s an example of how to set DOCKER_HOST in your code:

System.setProperty("DOCKER_HOST", "tcp://localhost:2375");
DockerClient dockerClient = DockerClientBuilder.getInstance().build();

Note that you may also need to enable TCP connections to Docker on your machine. To do this:

For Docker Desktop on Windows, go to Settings > Resources > Proxies and select "Expose daemon on tcp://localhost:2375 without TLS".
For Docker on Linux, edit the Docker daemon configuration file at /etc/docker/daemon.json and add "hosts": ["tcp://localhost:2375", "unix:///var/run/docker.sock"] to the "options" section.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading