Docker Remote api(一)

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("服务器端异常");
	         }
	        
	      }
	}

}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值