附官方api地址:https://docs.docker.com/engine/api/v1.24/
注:此方法仅仅创建了容器,并没有启动
/**
* 创建容器
* @param name 容器名称
* @param image 镜像
*@param port 端口
* @return
*
*/
public static ObjectVo createDocker(String name, String image,Integer port) {
String origin = "http://127.0.0.1:4243"; //http://+docker所在服务器的ip+docker开放的远程操作的端口
ObjectVo vo = new ObjectVo();
if(StringUtils.isBlank(name)||StringUtils.isBlank(image)){
vo.setCode(0);
vo.setMsg("参数不能为空!");
vo.setObject(null);
return vo;
}
String url = origin + "/containers/create";
Map<String, String> param = new HashMap<>(50);
param.put("name", name);
try {
ObjectVo objectVo = HttpClientUtil.doJSONRequest(url, getJSON(image,port), param);
if (null != objectVo) {
vo.setCode(objectVo.getCode());
vo.setMsg(objectVo.getMsg());
vo.setObject(objectVo.getObject());
}else{
vo.setCode(0);
vo.setMsg("请求出错,没有返回结果");
}
} catch (Exception e) {
e.printStackTrace();
}
return vo;
}
返回状态码,ObjectVo.code的值可以与状态码对比。
201 – no error
400 – bad parameter
404 – no such container
406 – impossible to attach (container not running)
409 – conflict
500 – server error
对docker操作的http请求
/**
* 带参数的HttpClient Post请求
* @param url
* @param json
* @param param
* @return
*/
public static ObjectVo doJSONRequest(String url, String json, Map<String, String> param) {
//创建HTTP客户端
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
ObjectVo objectVo = new ObjectVo();
try {
URIBuilder builder = new URIBuilder(url);
//创建参数列表
if (param != null) {
for (String key : param.keySet()) {
builder.addParameter(key, param.get(key));
}
}
URI uri = builder.build();
// 创建Http Post请求
HttpPost httpPost = new HttpPost(uri);
// 创建请求内容 即设置请求参数
StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
httpPost.setEntity(entity);
// 执行http请求
response = httpClient.execute(httpPost);
//获取响应状态码
objectVo.setCode(response.getStatusLine().getStatusCode());
String msg = null;
if (null != response.getEntity()) {
//接收响应头
msg = EntityUtils.toString(response.getEntity(), "UTF-8");
}
if (null != msg && !msg.trim().equals("")) {
JsonNode node = null;
try {
node = JSONUtil.getMapper().readTree(msg);
} catch (JsonParseException e) {
e.printStackTrace();
}
objectVo.setObject(node);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
//释放连接
response.close();
httpClient.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return objectVo;
}
创建docker容器的配置文件
/**
* 容器的一些常规配置
* @param image 镜像
* @return
*/
public static String getJSON(String image,Integer port) {
String json = "{\n" +
" \"Hostname\": \"\",\n" +
" \"Domainname\": \"\",\n" +
" \"User\": \"\",\n" +
" \"AttachStdin\": true,\n" +
" \"AttachStdout\": true,\n" +
" \"AttachStderr\": true,\n" +
" \"Tty\": false,\n" +
" \"OpenStdin\": true,\n" +
" \"StdinOnce\": false,\n" +
" \"Env\": [\n" +
" \"LANG=zh_CN.utf8\",\n" +
" \"BAZ=quux\"\n" +
" ],\n" +
" \"Cmd\": [\n" +
" \"\"\n" +
" ],\n" +
" \"Entrypoint\": \"\",\n" +
" \"Image\": \"IMAGE_TAG_REPLACE\",\n" +
" \"Labels\": {\n" +
" \"com.example.vendor\": \"Acme\",\n" +
" \"com.example.license\": \"GPL\",\n" +
" \"com.example.version\": \"1.0\"\n" +
" },\n" +
" \"WorkingDir\": \"\",\n" +
" \"NetworkDisabled\": false,\n" +
" \"ExposedPorts\": {\n" +
" \"6081/tcp\": {}\n" +
" },\n" +
" \"StopSignal\": \"SIGTERM\",\n" +
" \"StopTimeout\": 10,\n" +
" \"HostConfig\": {\n" +
" \"Binds\": [\n" +
" \"\"\n" +
" ],\n" +
" \"PortBindings\": {\n" +
" \"6081/tcp\": [\n" +
" {\n" +
" \"HostPort\": \"HOST_PORT_REPLACE\"\n" +
" }\n" +
" ]\n "+
" },\n" +
" \"PublishAllPorts\": false,\n" +
" \"Privileged\": false,\n" +
" \"ReadonlyRootfs\": false,\n" +
" \"CapAdd\": [\n" +
" \"NET_ADMIN\"\n" +
" ],\n" +
" \"CapDrop\": [\n" +
" \"MKNOD\"\n" +
" ],\n" +
" \"NetworkMode\": \"bridge\",\n" +
" \"Devices\": [],\n" +
" \"LogConfig\": {\n" +
" \"Type\": \"json-file\",\n" +
" \"Config\": {}\n" +
" }\n" +
" }\n" +
"}";
json = json.replace("IMAGE_TAG_REPLACE", image)
.replace("HOST_PORT_REPLACE", port.toString());
return json;
}
@Data
public class ObjectVo {
private Integer code;
private String msg;
private Object object;
}