1、总的来说github上发布的docker remote api的上层调用还是不错的,但是有些jar的问
题很是麻烦,最好还是自己来直接调用其Rest API
2、查了好久关于如何create一个container,功夫不负有心人,还是找到了解决办法。
3、代码例子
package cn.ac.iie.bms.test;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.MediaType;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.UniformInterfaceException;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.core.util.MultivaluedMapImpl;
public class test {
public static void main(String[] args) throws JSONException {
JSONObject obj1 = new JSONObject();
obj1.put("Image", "registry");
JSONArray jsonArray = new JSONArray();
JSONObject obj2 = new JSONObject();
obj2.put("HostPort", "5000");
jsonArray.put(obj2);
JSONObject obj3 = new JSONObject();
obj3.put("5000/tcp", jsonArray);
JSONObject obj4= new JSONObject();
obj4.put("PortBindings", obj3);
obj1.put("HostConfig", obj4);
System.out.println(obj1.toString());
MultivaluedMapImpl params = new MultivaluedMapImpl();
params.add("name", "registry");
ClientConfig cc = new DefaultClientConfig();
Client client = Client.create(cc);
WebResource webResource = client.resource("http://192.168.110.136:5678/containers/create?name=registry");
try {
ClientResponse response = webResource.accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON).post(ClientResponse.class, obj1);
String reponse = response.getEntity(String.class);
System.out.println(response);
} catch (UniformInterfaceException e) {
ClientResponse response = e.getResponse();
if( response.getStatus()==201){
System.out.println("容器创建成功!!");
} else if(response.getStatus()==400){
System.out.println("参数错误");
}else if(response.getStatus()==500){
System.out.println("服务器端异常");
}
}
}
}