如何使用docker-java API连接docker? [英] How to use docker-java API to connect docker?
问题描述
docker -H tcp://192.168.59.103:2376 version
它可以成功并向我显示此信息:
code>客户端版本:1.7.0
客户端API版本:1.19
Go版本(客户端):go1.4.2
Git提交(客户端):0baf609
操作系统/ Arch(客户端):darwin / amd64
服务器版本:1.7.0
服务器API版本:1.19
转到版本(服务器):go1.4.2
Git提交(服务器) 0baf609
OS / Arch(服务器):linux / amd64
然后,我新的一个maven项目在eclipse,并运行这个代码:
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);
}
但是,它根本没有工作,并抛出关于这个的异常:
服务器无法使用有效的HTTP响应响应
而且,我使用命令curl连接:
curl -v http://192.168.59.103:2376/info
它显示我的信息如下:
* DNS缓存中未找到主机名
*尝试192.168.59.103 ...
*连接到192.168。 59.103(192.168.59.103)港2376(#0)
> GET / info HTTP / 1.1
>用户代理:curl / 7.37.1
>主持人:192.168.59.103:2376
>接受:* / *
>
*连接#0到主机192.168.59.103保持原样
我在这种情况下呢?我想使用它来显示一些docker信息,并使用java代码对docker进行某些操作。
更多java异常细节是:
线程main中的异常javax.ws.rs.ProcessingException:org。 apache.http.client.ClientProtocolException $ org.bache.apache.connector.ApacheConnector.apply(ApacheConnector.java:513)中的
$ org.glassfish.jersey.client.ClientRuntime.invoke(ClientRuntime .java:246)
在org.glassfish.jersey.client.JerseyInvocation $ 2.call(JerseyInvocation.java:683)
在org.glassfish.jersey.internal.Errors.process(Errors.java: 315)
在org.glassfish.jersey.internal.Errors.process(Errors.java:297)
在org.glassfish.jersey.internal.Errors.process(Errors.java:228)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:424)
在org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:679)
在org.glassfish.jersey.client.JerseyInvocation $ Builder.method(JerseyInvocation.java:408)
在org.glassfish.jersey.client.JerseyI nvocation $ Builder.get(JerseyInvocation.java:308)
在com.github.dockerjava.jaxrs.InfoCmdExec.execute(InfoCmdExec.java:26)
在com.github.dockerjava.jaxrs.InfoCmdExec。执行(InfoCmdExec.java:12)
在com.github.dockerjava.jaxrs.AbstrDockerCmdExec.exec(AbstrDockerCmdExec.java:57)
在com.github.dockerjava.core.command.AbstrDockerCmd.exec( AbstrDockerCmd.java:29)
在org.v11.dm.docker.Demo.main(Demo.java:15)
导致:org.apache.http.client.ClientProtocolException
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:188)
在org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:72)
在org.apache.http.ProtocolException:服务器无法响应一个有效的HTTP响应
在org.apache.http.impl.conn.DefaultHttpResponseParser.pars eHead(DefaultHttpResponseParser.java:151)
在org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:57)
在org.apache.http.impl.io.AbstractMessageParser。 parse(AbstractMessageParser.java:260)
在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)
java.lang.reflect.Method.invoke( Method.java:497)
在org.apache.http.impl.conn.CPoolProxy.invoke(CPoolProxy.java:138)
在com.sun.proxy $ Proxy19.receiveResponseHeader(未知来源)
在org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:271)
在org.apache.http.protocol.HttpRequestExecut or.execute(HttpRequestExecutor.java:123)
在org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:253)
在org.apache.http.impl.execchain。 ProtocolExec.execute(ProtocolExec.java:194)
在org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:85)
在org.apache.http.impl.execchain。 RedirectExec.execute(RedirectExec.java:108)
在org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:186)
... 16更多
您的代码为我工作。
我有以下设置 -
-
Docker守护程序正在使用此命令运行 -
/ usr / bin / docker -d -H fd:// -H tcp://0.0.0.0:2375
-
我可以卷曲 -
curl http:// localhost:2375 / info
-
还可以运行您的代码。
注意
如果您使用的是boot2docker,请检查守护程序的运行方式,并接受http请求。如果它使用TLSVerify运行,请停止运行,如第一步所述。
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.
这篇关于如何使用docker-java API连接docker?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!