How to use docker-java API to connect docker?

I am new to docker and I want to use java api to print docker info. Then I find an Api in this one about docker-java. And I found my boot2docker ip is 196.168.59.103:2376. And I am using this command:

docker -H tcp://192.168.59.103:2376 version

      

This can succeed and show me this information:

Client version: 1.7.0
Client API version: 1.19
Go version (client): go1.4.2
Git commit (client): 0baf609
OS/Arch (client): darwin/amd64
Server version: 1.7.0
Server API version: 1.19
Go version (server): go1.4.2
Git commit (server): 0baf609
OS/Arch (server): linux/amd64

      

Then I re-create the maven project in eclipse and run this code:

public static void main(String[] args) {
        DockerClient dockerClient = DockerClientBuilder.getInstance("http://192.168.59.103:2376").build();
        Info info = dockerClient.infoCmd().exec();
        System.out.print(info);
    }

      

But that didn't work at all and throws an exception:

The server failed to respond with a valid HTTP response

      

And I am using curl to connect:

curl -v http://192.168.59.103:2376/info

      

Show me the information below:

* Hostname was NOT found in DNS cache
*   Trying 192.168.59.103...
* Connected to 192.168.59.103 (192.168.59.103) port 2376 (#0)
> GET /info HTTP/1.1
> User-Agent: curl/7.37.1
> Host: 192.168.59.103:2376
> Accept: */*
> 

* Connection #0 to host 192.168.59.103 left intact

      

How should I do in this case? I want to use this to show some docker information and do docker stuff using Java code. more details about java exception:

Exception in thread "main" javax.ws.rs.ProcessingException: org.apache.http.client.ClientProtocolException
    at org.glassfish.jersey.apache.connector.ApacheConnector.apply(ApacheConnector.java:513)
    at org.glassfish.jersey.client.ClientRuntime.invoke(ClientRuntime.java:246)
    at org.glassfish.jersey.client.JerseyInvocation$2.call(JerseyInvocation.java:683)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:228)
    at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:424)
    at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:679)
    at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:408)
    at org.glassfish.jersey.client.JerseyInvocation$Builder.get(JerseyInvocation.java:308)
    at com.github.dockerjava.jaxrs.InfoCmdExec.execute(InfoCmdExec.java:26)
    at com.github.dockerjava.jaxrs.InfoCmdExec.execute(InfoCmdExec.java:12)
    at com.github.dockerjava.jaxrs.AbstrDockerCmdExec.exec(AbstrDockerCmdExec.java:57)
    at com.github.dockerjava.core.command.AbstrDockerCmd.exec(AbstrDockerCmd.java:29)
    at org.v11.dm.docker.Demo.main(Demo.java:15)
Caused by: org.apache.http.client.ClientProtocolException
    at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:188)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:72)
    at org.glassfish.jersey.apache.connector.ApacheConnector.apply(ApacheConnector.java:465)
    ... 14 more
Caused by: org.apache.http.ProtocolException: The server failed to respond with a valid HTTP response
    at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:151)
    at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:57)
    at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:260)
    at org.apache.http.impl.DefaultBHttpClientConnection.receiveResponseHeader(DefaultBHttpClientConnection.java:161)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.apache.http.impl.conn.CPoolProxy.invoke(CPoolProxy.java:138)
    at com.sun.proxy.$Proxy19.receiveResponseHeader(Unknown Source)
    at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:271)
    at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:123)
    at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:253)
    at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:194)
    at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:85)
    at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:108)
    at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:186)
    ... 16 more

      

+3


source to share


1 answer


Your code worked for me.

I have the following setup -

  • Docker daemon works with this command -

    /usr/bin/docker -d -H fd:// -H tcp://0.0.0.0:2375

  • I can curl -

    curl http://localhost:2375/info

  • You can also run your code.



Note

If you are using boot2docker, check how the daemon is running and if it accepts an HTTP request. If it works with TLSVerify, stop it and start it as I mentioned in the first step.

0


source







All Articles