Apache Connection Refused when running Docker-clie

2019-05-23 10:39发布

问题:

I am trying to install the Docker-client Remote API library ( https://github.com/spotify/docker-client ) to do some image searches and inspect image data (all in public repositories). I have the boot2docker VM downloaded, installed and running. Commands such as "Docker pull ubuntu" work fine but I would like to do this via a Java program now. I used the Eclipse IDE Egit plugin to import the github project and created a Maven/Java project from the existing Master branch. The source code is completely imported and no errors reported. I then tried writing a simple test:

    final DockerClient docker = DefaultDockerClient.fromEnv().build();
    //docker.pull("busybox");
    List<ImageSearchResult> results = docker.searchImages("ubuntu");
    for (ImageSearchResult res : results) {
        System.out.println(res.getName());
    }

However, when running the code in Eclipse I get the following error:

Exception in thread "main" com.spotify.docker.client.DockerException: java.util.concurrent.ExecutionException: javax.ws.rs.ProcessingException: org.apache.http.conn.HttpHostConnectException: Connect to localhost:2375 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused: connect
at com.spotify.docker.client.DefaultDockerClient.propagate(DefaultDockerClient.java:1109)
at com.spotify.docker.client.DefaultDockerClient.request(DefaultDockerClient.java:1028)
at com.spotify.docker.client.DefaultDockerClient.searchImages(DefaultDockerClient.java:653)
at com.spotify.docker.client.main.Test.main(Test.java:28)

I tried setting up an apache server on that port but then I get:

Exception in thread "main" com.spotify.docker.client.DockerRequestException: Request error: GET http://localhost:2375/v1.12/images/search?term=ubuntu: 404
at com.spotify.docker.client.DefaultDockerClient.propagate(DefaultDockerClient.java:1100)
at com.spotify.docker.client.DefaultDockerClient.request(DefaultDockerClient.java:1028)
at com.spotify.docker.client.DefaultDockerClient.searchImages(DefaultDockerClient.java:653)
at com.spotify.docker.client.main.Test.main(Test.java:28)

Can anyone tell me what I am supposed to do here to make my search/pull call work? This is my first try with Docker and I've searched through the documentation and googled the problem but can't find anyone with a similar problem.

Thank you!

EDIT: I am running docker in Windows 7 via the pre-built VM Boot2Docker. Maybe the Docker daemon running inside that is not accessible from programs outside of the VM such as Eclipse?

EDIT: solved it by upgrading to v1.6 instead of v1.5 which makes the daemon available in the Windows host. Current problem is that all my API calls are returning "The server failed to respond with a valid HTTP response"

回答1:

I encountered a similar issue and I managed to solve this issue by using the following way, to build up the DockerClient:

final DockerClient docker = DefaultDockerClient.builder()
                    .uri(URI.create("unix:///var/run/docker.sock"))
                    .build();

I had been getting the same exception but adding the above URI part helped me to solve the issue. A better explanation for a issue similar to the above and how to solve it has been provided in the following issue tracker.

https://github.com/spotify/docker-maven-plugin/issues/61



回答2:

The Java program does essentially a docker search: that can only work in an environment where the docker engine is present.

Either in the boot2docker VM.
Or in a full Linux host.



回答3:

I did encounter the same problem on Mac with eclipse and Docker version 1.10.3, I did search for a solution before I settled for a workaround - Using docker CLI docker-manager to create a new virtualbox and get the DOCKER_HOST and DOCKER_CERT_PATH values of that virtualbox and create a new builder.

In my case: I have created a virtual box default2 using docker CLI command docker-machine create -d virtualbox default2

Docker CLI

$ docker-machine env
export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://192.168.99.103:2376"
export DOCKER_CERT_PATH="/Users/XXXX/.docker/machine/machines/default2"
export DOCKER_MACHINE_NAME="default2"

Docker-client JAVA

DockerCertificates defaultCertificates = new DockerCertificates(Paths.get("/Users/XXXX/.docker/machine/machines/default2"));    
DockerClient docker = DefaultDockerClient.builder()
                .uri("https://192.168.99.103:2376")
                .dockerCertificates(defaultCertificates)
                .build();