Docker文档地址:https://any-api.com/docker_com/engine/docs/Definitions/TaskSpec
Docker Hub API文档地址:https://docs.docker.com/docker-hub/api/latest/#
Docker Engine API 文档地址:https://docs.docker.com/engine/api/sdk/
下面代码使用Java实现了部分Docker API接口功能:
package Docker;import org.json.JSONArray;import org.json.JSONException;import org.json.JSONObject;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintWriter;import java.net.HttpURLConnection;import java.net.URL;import java.net.URLConnection;import java.util.List;import java.util.Map;public class DockerAPI {public static void main(String[] args) throws JSONException {SetDockerHostAddr("http://192.168.20.148:2375");Docker_Services_create("nginx", "nginx:latest", 1,8085, 80);JSONArray array = Docker_Services_1();String id = array.getJSONObject(0).get("ID").toString();System.out.printf("ID = %s\n", id);Docker_Services_delete(id);}public static void Test_DockerAPI() throws JSONException {// 设置请求地址SetDockerHostAddr("http://192.168.20.148:2375");// 初始化SwarmDocker_Swarm_init("192.168.20.148:2377", 4789);// 获取服务列表Docker_Services_1();Docker_Services_1("6jz5z5d2o8a5");// 创建服务Docker_Services_create("nginx", "nginx:latest", 1,8085, 80);// 删除服务Docker_Services_delete("jyudhbtsqqtw");// 列出容器Docker_Containers_list();// 检查容器Docker_Containers_1("67e99b224f2d");// 启动容器Docker_Containers_start("67e99b224f2d");// 停止容器Docker_Containers_stop("67e99b224f2d", 3);// 重启容器Docker_Containers_restart("67e99b224f2d", 3);// 杀死容器Docker_Containers_kill("67e99b224f2d");// 暂停容器Docker_Containers_pause("67e99b224f2d");// 恢复容器Docker_Containers_unpause("67e99b224f2d");// 查看集群JSONObject obj_swarm = Docker_Swarm_1();System.out.printf("ID = %s\n", obj_swarm.get("ID").toString());// 加入集群SetDockerHostAddr("http://192.168.20.148:2375");Docker_Swarm_join("192.168.20.148:2375", "192.168.20.222:2377", "SWMTKN-1-3wygqrwkee9kg1x902hrotbn0j8monb55dwu9f810dlyf7t8l2-ac7hzwwnn92176bjolvr36fbc");// 查询节点Docker_Nodes_1("xuza3s1zc2ieybiro77l5hnm1");// 获取镜像列表Docker_Image_list();// 镜像检查Docker_Image_1("redis");// PingDocker_ping();// 获取版本信息Docker_version();// 获取Info信息Docker_info();// 获取系统信息Docker_System_df();// 获取配置信息Docker_Configs_1();// 离开SwarmDocker_Swarm_leave();}////////////////////////////////////////////////////////////////////////////////////////////////////////////////////private static String HostAddr;public static void SetDockerHostAddr(String hostAddr) {HostAddr = hostAddr; // "http://192.168.20.222:2375";}////////////////////////////////////////////////////////////////////////////////////////////////////////////////////public static String Docker_Swarm_init(String addr, Integer port) throws JSONException {System.out.printf("-->> [Docker_Swarm_init]\n");System.out.printf("-->> [Request way] : POST\n");System.out.printf("-->> [Request URL] : %s\n", HostAddr + "/swarm/init");JSONObject obj = new JSONObject();obj.put("ListenAddr", addr); // "192.168.20.222:2377"obj.put("AdvertiseAddr", addr);obj.put("DataPathPort", port); // 4789obj.put("DefaultAddrPool", new JSONArray().put("10.10.0.0/8").put("20.20.0.0/8"));obj.put("SubnetSize", 24);obj.put("ForceNewCluster", false);JSONObject obj_spec = new JSONObject();obj_spec.put("Orchestration", new JSONObject());obj_spec.put("Raft", new JSONObject());obj_spec.put("Dispatcher", new JSONObject());obj_spec.put("CAConfig", new JSONObject());obj_spec.put("EncryptionConfig", new JSONObject().put("AutoLockManagers", false));obj.put("Spec", obj_spec);System.out.printf("-->> [Request parameters] : %s\n", obj.toString());String response = SendPost(HostAddr + "/swarm/init", obj.toString());System.out.printf("-->> [Response data] : %s\n\n", response);return response;}public static JSONObject Docker_Swarm_1() throws JSONException {System.out.printf("-->> [Docker_Swarm_1]\n");System.out.printf("-->> [Request way] : GET\n");System.out.printf("-->> [Request URL] : %s\n", HostAddr + "/swarm");System.out.printf("-->> [Request parameters] : \n");String response = SendGet(HostAddr + "/swarm", "");System.out.printf("-->> [Response data] : %s\n\n", response);JSONObject obj = new JSONObject(response);return obj;}public static void Docker_Swarm_leave() {System.out.printf("-->> [Docker_Swarm_leave]\n");System.out.printf("-->> [Request way] : POST\n");System.out.printf("-->> [Request URL] : %s\n", HostAddr + "/swarm/leave?force=true");System.out.printf("-->> [Request parameters] : \n");String response = SendPost(HostAddr + "/swarm/leave?force=true", "");System.out.printf("-->> [Response data] : %s\n\n", response);}public static void Docker_Swarm_join(String hostAddr, String nodeAddr, String joinToken) throws JSONException {System.out.printf("-->> [Docker_Swarm_join]\n");System.out.printf("-->> [Request way] : POST\n");System.out.printf("-->> [Request URL] : %s\n", HostAddr + "/swarm/join");JSONObject obj = new JSONObject();obj.put("ListenAddr", hostAddr);obj.put("AdvertiseAddr", hostAddr);obj.put("RemoteAddrs", new JSONArray().put(nodeAddr));obj.put("JoinToken", joinToken);System.out.printf("-->> [Request parameters] : %s\n", obj.toString());String response = SendPost(HostAddr + "/swarm/join", obj.toString());System.out.printf("-->> [Response data] : %s\n\n", response);}////////////////////////////////////////////////////////////////////////////////////////////////////////////////////public static JSONArray Docker_Services_1() throws JSONException {System.out.printf("-->> [Docker_Services_1]\n");System.out.printf("-->> [Request way] : GET\n");System.out.printf("-->> [Request URL] : %s\n", HostAddr + "/services");System.out.printf("-->> [Request parameters] : \n");String response = SendGet(HostAddr + "/services", "");System.out.printf("-->> [Response data] : %s\n\n", response);JSONArray array = new JSONArray(response);return array;}public static JSONObject Docker_Services_1(String id) throws JSONException {System.out.printf("-->> [Docker_Services_1]\n");System.out.printf("-->> [Request way] : GET\n");System.out.printf("-->> [Request URL] : %s\n", HostAddr + "/services/" + id.toString());System.out.printf("-->> [Request parameters] : \n");String response = SendGet(HostAddr + "/services/" + id.toString(), "");System.out.printf("-->> [Response data] : %s\n\n", response);JSONObject obj = new JSONObject(response);return obj;}public static void Docker_Services_create(String name, String image, Integer replicas, Integer published_port, Integer target_port) throws JSONException {System.out.printf("-->> [Docker_Services_create]\n");System.out.printf("-->> [Request way] : POST\n");System.out.printf("-->> [Request URL] : %s\n", HostAddr + "/services/create");JSONObject obj = new JSONObject();obj.put("Name", name); //nginxJSONObject obj_TaskTemplate = new JSONObject();JSONObject obj_ContainerSpec = new JSONObject();obj_ContainerSpec.put("Image", image.toString()); // nginx:alpineobj_ContainerSpec.put("Hosts", new JSONArray().put("10.10.10.10 host1").put("ABCD:EF01:2345:6789:ABCD:EF01:2345:6789 host2"));obj_TaskTemplate.put("ContainerSpec", obj_ContainerSpec);JSONObject obj_Limits = new JSONObject();obj_Limits.put("Limits", new JSONObject().put("NanoCPUs", 1000000).put("MemoryBytes", 104857600));obj_TaskTemplate.put("Resources", obj_Limits);obj.put("TaskTemplate", obj_TaskTemplate);JSONObject obj_Mode = new JSONObject();obj_Mode.put("Replicated", new JSONObject().put("Replicas", replicas)); // 1obj.put("Mode", obj_Mode);JSONArray obj_Ports = new JSONArray();obj_Ports.put(new JSONObject().put("Protocol", "tcp").put("PublishedPort", published_port).put("TargetPort", target_port));obj.put("EndpointSpec", new JSONObject().put("Ports", obj_Ports));System.out.printf("-->> [Request parameters] : %s\n", obj.toString());String response = SendPost(HostAddr + "/services/create", obj.toString());System.out.printf("-->> [Response data] : %s\n\n", response);}public static void Docker_Services_delete(String id) {System.out.printf("-->> [Docker_Services_delete]\n");System.out.printf("-->> [Request way] : DELETE\n");System.out.printf("-->> [Request URL] : %s\n", HostAddr + "/services/" + id.toString());System.out.printf("-->> [Request parameters] : \n");String response = SendDelete(HostAddr + "/services/" + id.toString());System.out.printf("-->> [Response data] : %s\n\n", response);}////////////////////////////////////////////////////////////////////////////////////////////////////////////////////public static JSONArray Docker_Containers_list() throws JSONException {System.out.printf("-->> [Docker_Containers_list]\n");System.out.printf("-->> [Request way] : GET\n");System.out.printf("-->> [Request URL] : %s\n", HostAddr + "/containers/json");System.out.printf("-->> [Request parameters] : \n");String response = SendGet(HostAddr + "/containers/json", "");System.out.printf("-->> [Response data] : %s\n\n", response);JSONArray array = new JSONArray(response);return array;}public static JSONObject Docker_Containers_1(String id) throws JSONException {System.out.printf("-->> [Docker_Containers_1]\n");System.out.printf("-->> [Request way] : GET\n");System.out.printf("-->> [Request URL] : %s\n", HostAddr + "/containers/" + id.toString() + "/json");System.out.printf("-->> [Request parameters] : \n");String response = SendGet(HostAddr + "/containers/" + id.toString() + "/json", "");System.out.printf("-->> [Response data] : %s\n\n", response);JSONObject obj = new JSONObject(response);return obj;}public static void Docker_Containers_start(String id) {System.out.printf("-->> [Docker_Container_start]\n");System.out.printf("-->> [Request way] : POST\n");System.out.printf("-->> [Request URL] : %s\n", HostAddr + "/containers/" + id + "/start");JSONObject obj = new JSONObject();String response = SendPost(HostAddr + "/containers/" + id + "/start", obj.toString());System.out.printf("-->> [Response data] : %s\n\n", response);}public static void Docker_Containers_stop(String id, Integer second) {System.out.printf("-->> [Docker_Containers_stop]\n");System.out.printf("-->> [Request way] : POST\n");System.out.printf("-->> [Request URL] : %s\n", HostAddr + "/containers/" + id + "/stop");JSONObject obj = new JSONObject();String response = SendPost(HostAddr + "/containers/" + id + "/stop?t=" + second.toString(), obj.toString());System.out.printf("-->> [Response data] : %s\n\n", response);}public static void Docker_Containers_restart(String id, Integer second) {System.out.printf("-->> [Docker_Containers_restart]\n");System.out.printf("-->> [Request way] : POST\n");System.out.printf("-->> [Request URL] : %s\n", HostAddr + "/containers/" + id + "/restart");JSONObject obj = new JSONObject();String response = SendPost(HostAddr + "/containers/" + id + "/restart?t=" + second.toString(), obj.toString());System.out.printf("-->> [Response data] : %s\n\n", response);}public static void Docker_Containers_kill(String id) {System.out.printf("-->> [Docker_Containers_kill]\n");System.out.printf("-->> [Request way] : POST\n");System.out.printf("-->> [Request URL] : %s\n", HostAddr + "/containers/" + id + "/kill");JSONObject obj = new JSONObject();String response = SendPost(HostAddr + "/containers/" + id + "/kill", obj.toString());System.out.printf("-->> [Response data] : %s\n\n", response);}public static void Docker_Containers_pause(String id) {System.out.printf("-->> [Docker_Containers_pause]\n");System.out.printf("-->> [Request way] : POST\n");System.out.printf("-->> [Request URL] : %s\n", HostAddr + "/containers/" + id + "/pause");JSONObject obj = new JSONObject();String response = SendPost(HostAddr + "/containers/" + id + "/pause", obj.toString());System.out.printf("-->> [Response data] : %s\n\n", response);}public static void Docker_Containers_unpause(String id) {System.out.printf("-->> [Docker_Containers_unpause]\n");System.out.printf("-->> [Request way] : POST\n");System.out.printf("-->> [Request URL] : %s\n", HostAddr + "/containers/" + id + "/unpause");JSONObject obj = new JSONObject();String response = SendPost(HostAddr + "/containers/" + id + "/unpause", obj.toString());System.out.printf("-->> [Response data] : %s\n\n", response);}////////////////////////////////////////////////////////////////////////////////////////////////////////////////////public static JSONObject Docker_Nodes_1(String id) throws JSONException {System.out.printf("-->> [Docker_Nodes_1]\n");System.out.printf("-->> [Request way] : GET\n");System.out.printf("-->> [Request URL] : %s\n", HostAddr + "/nodes" + id.toString());System.out.printf("-->> [Request parameters] : \n");String response = SendGet(HostAddr + "/nodes/" + id.toString(), "");System.out.printf("-->> [Response data] : %s\n\n", response);JSONObject obj = new JSONObject(response);return obj;}////////////////////////////////////////////////////////////////////////////////////////////////////////////////////public static JSONArray Docker_Image_list() throws JSONException {System.out.printf("-->> [Docker_Image_list]\n");System.out.printf("-->> [Request way] : GET\n");System.out.printf("-->> [Request URL] : %s\n", HostAddr + "/images/json");System.out.printf("-->> [Request parameters] : \n");String response = SendGet(HostAddr + "/images/json", "");System.out.printf("-->> [Response data] : %s\n\n", response);JSONArray array = new JSONArray(response);return array;}public static JSONObject Docker_Image_1(String name_or_id) throws JSONException {System.out.printf("-->> [Docker_Image_1]\n");System.out.printf("-->> [Request way] : GET\n");System.out.printf("-->> [Request URL] : %s\n", HostAddr + "/images/" + name_or_id + "/json");System.out.printf("-->> [Request parameters] : \n");String response = SendGet(HostAddr + "/images/" + name_or_id + "/json", "");System.out.printf("-->> [Response data] : %s\n\n", response);JSONObject obj = new JSONObject(response);return obj;}public static void Docker_Image_export(String name) {System.out.printf("-->> [Docker_Image_export]\n");System.out.printf("-->> [Request way] : GET\n");System.out.printf("-->> [Request URL] : %s\n", HostAddr + "/images/" + name + "/get");System.out.printf("-->> [Request parameters] : \n");String response = SendGet(HostAddr + "/images/" + name + "/get", "");System.out.printf("-->> [Response data] : %s\n\n", response);}////////////////////////////////////////////////////////////////////////////////////////////////////////////////////public static String Docker_ping() {System.out.printf("-->> [Docker_ping]\n");System.out.printf("-->> [Request way] : GET\n");System.out.printf("-->> [Request URL] : %s\n", HostAddr + "/_ping");System.out.printf("-->> [Request parameters] : \n");String response = SendGet(HostAddr + "/_ping", "");System.out.printf("-->> [Response data] : %s\n\n", response);return response;}public static JSONObject Docker_version() throws JSONException {System.out.printf("-->> [Docker_version]\n");System.out.printf("-->> [Request way] : GET\n");System.out.printf("-->> [Request URL] : %s\n", HostAddr + "/version");System.out.printf("-->> [Request parameters] : \n");String response = SendGet(HostAddr + "/version", "");System.out.printf("-->> [Response data] : %s\n\n", response);JSONObject obj = new JSONObject(response);return obj;}public static JSONObject Docker_info() throws JSONException {System.out.printf("-->> [Docker_info]\n");System.out.printf("-->> [Request way] : GET\n");System.out.printf("-->> [Request URL] : %s\n", HostAddr + "/info");System.out.printf("-->> [Request parameters] : \n");String response = SendGet(HostAddr + "/info", "");System.out.printf("-->> [Response data] : %s\n\n", response);JSONObject obj = new JSONObject(response);return obj;}public static JSONObject Docker_System_df() throws JSONException {System.out.printf("-->> [Docker_System_df]\n");System.out.printf("-->> [Request way] : GET\n");System.out.printf("-->> [Request URL] : %s\n", HostAddr + "/system/df");System.out.printf("-->> [Request parameters] : \n");String response = SendGet(HostAddr + "/system/df", "");System.out.printf("-->> [Response data] : %s\n\n", response);JSONObject obj = new JSONObject(response);return obj;}////////////////////////////////////////////////////////////////////////////////////////////////////////////////////public static JSONArray Docker_Configs_1() throws JSONException {System.out.printf("-->> [Docker_Configs_1]\n");System.out.printf("-->> [Request way] : GET\n");System.out.printf("-->> [Request URL] : %s\n", HostAddr + "/configs");System.out.printf("-->> [Request parameters] : \n");String response = SendGet(HostAddr + "/configs", "");System.out.printf("-->> [Response data] : %s\n\n", response);JSONArray array = new JSONArray(response);return array;}////////////////////////////////////////////////////////////////////////////////////////////////////////////////////public static String SendGet(String url, String param) {String result = "";BufferedReader in = null;try {String urlNameString = url + "?" + param;URL realUrl = new URL(urlNameString);// 打开和URL之间的连接URLConnection connection = realUrl.openConnection();// 设置通用的请求属性connection.setRequestProperty("accept", "*/*");connection.setRequestProperty("connection", "Keep-Alive");connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSE 6.0; Windows NT 5.1;SV1)");// 建立实际的连接connection.connect();// 获取所有响应头字段Map<String, List<String>> map = connection.getHeaderFields();// // 遍历所有的响应头字段// for (String key : map.keySet()) {// System.out.println(key + "--->" + map.get(key));// }// 定义 BufferedReader输入流来读取URL的响应in = new BufferedReader(new InputStreamReader(connection.getInputStream()));String line;while ((line = in.readLine()) != null) {result += line;}} catch (Exception e) {System.out.println("GET Request Exception! " + e);e.printStackTrace();}// 使用finally块来关闭输入流finally {try {if (in != null) {in.close();}} catch (Exception e2) {e2.printStackTrace();}}return result;}public static String SendPost(String url, String param) {PrintWriter out = null;BufferedReader in = null;String result = "";try {URL realUrl = new URL(url);// 打开和URL之间的连接URLConnection conn = realUrl.openConnection();// 设置通用的请求属性conn.setRequestProperty("accept", "*/*");conn.setRequestProperty("connection", "Keep-Alive");conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSE 6.0; Windows NT 5.1;SV1)");// 发送POST请求必须设置如下两行conn.setDoOutput(true);conn.setDoInput(true);// 获取URLConnection对象对应的输出流out = new PrintWriter(conn.getOutputStream());// 发送请求参数out.print(param);// flush输出流的缓冲out.flush();// 定义BufferedReader输入流来读取URL的响应in = new BufferedReader(new InputStreamReader(conn.getInputStream()));String line;while ((line = in.readLine()) != null) {result += line;}} catch (Exception e) {System.out.println("POST Request Exception! " + e);e.printStackTrace();}//使用finally块来关闭输出流、输入流finally {try {if (out != null) {out.close();}if (in != null) {in.close();}} catch (IOException ex) {ex.printStackTrace();}}return result;}public static String SendDelete(String url) {PrintWriter out = null;BufferedReader in = null;String result = "";try {URL realUrl = new URL(url);HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection();conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");conn.setRequestMethod("DELETE");// System.out.println(conn.getRequestProperties());// 发送POST请求必须设置如下两行conn.setDoOutput(true);conn.setDoInput(true);// 获取URLConnection对象对应的输出流out = new PrintWriter(conn.getOutputStream());// flush输出流的缓冲out.flush();// 定义BufferedReader输入流来读取URL的响应in = new BufferedReader(new InputStreamReader(conn.getInputStream()));String line;while ((line = in.readLine()) != null) {result += line;}} catch (Exception e) {System.out.println("POST Request Exception! " + e);e.printStackTrace();}//使用finally块来关闭输出流、输入流finally {try {if (out != null) {out.close();}if (in != null) {in.close();}} catch (IOException ex) {ex.printStackTrace();}}return result;}}
