docker api java,如何使用docker-java API连接docker?

I'm new to docker, and I want to use java api to print my docker information. Then I find the Api in this link about docker-java. And I found my boot2docker ip is 196.168.59.103:2376. And I use this command:

docker -H tcp://192.168.59.103:2376 version

It 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 new a 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, it didn't work at all, and throw the exception about this:

The server failed to respond with a valid HTTP response

And,I use command curl to connect:

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

It show me information is 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 something to docker using java code.

more java exception detail is:

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

解决方案

Your code worked for me.

I have following setup -

Docker daemon is running with this command -

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

I am able to curl -

curl http://localhost:2375/info

Also able to run your code.

NOTE

If you are using boot2docker, please check how daemon is running and is it accepting http request. If it's running with TLSVerify please stop it and run as i mentioned in 1st step.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值