2

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"

user134589
  • 2,499
  • 2
  • 16
  • 12
  • Did you run your Java program on the boot2docker TinyCore VM Linux host? – VonC Apr 28 '15 at 13:54
  • No, I am running it outside of it in Windows 7 in Eclipse. I just started realising: the Docker daemon is not accessible outside of the VM is it? How would I even be able to get my Java program running in the VM? I guess the easiest option is to just install docker on linux and work from there. – user134589 Apr 28 '15 at 13:56
  • 1
    Exactly: you would need to run your java program (which does a `docker search`) where the docker engine is: in the Linux host (the boot2docker VM) – VonC Apr 28 '15 at 14:00

3 Answers3

3

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

Chiranga Alwis
  • 1,049
  • 1
  • 25
  • 47
1

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.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • there is docker client for windows now - so another option is to install it and configure to use docker server in vm. – ISanych Apr 28 '15 at 14:11
  • @ISanych not really possible: there is no persistence in the TinyCore VM: http://stackoverflow.com/a/29507588/6309 – VonC Apr 28 '15 at 14:21
  • It has nothing to do with persistence, docker client only need socket connection to server which could be specified in environment variable: https://docs.docker.com/installation/windows/ – ISanych Apr 28 '15 at 14:44
  • @Isanych I tried following the "Using docker from Windows Command Line Prompt (cmd.exe)" but it just says "boot2docker is not available for windows now, please use ssh to log in to the VM" – user134589 Apr 29 '15 at 06:23
  • @user134589 what command did you type? (from https://docs.docker.com/installation/windows/#using-docker-from-windows-command-line-prompt-cmdexe) – VonC Apr 29 '15 at 06:27
  • Follow up on my own comment: upgrading from v1.5 to v1.6 seemed to resolve that, it's a brand new feature apparantely. Maybe this will allow me to connect to the daemon in Windows. – user134589 Apr 29 '15 at 06:50
0

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();
Sudhakar
  • 43
  • 9