DockerTest.java 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. package com.miniframe;
  2. import com.github.dockerjava.api.DockerClient;
  3. import com.github.dockerjava.api.async.ResultCallback;
  4. import com.github.dockerjava.api.command.CreateContainerResponse;
  5. import com.github.dockerjava.api.command.InspectContainerResponse;
  6. import com.github.dockerjava.api.command.LogContainerCmd;
  7. import com.github.dockerjava.api.command.PullImageResultCallback;
  8. import com.github.dockerjava.api.model.*;
  9. import com.github.dockerjava.core.DefaultDockerClientConfig;
  10. import com.github.dockerjava.core.DockerClientBuilder;
  11. import com.github.dockerjava.core.DockerClientConfig;
  12. import com.github.dockerjava.core.command.LogContainerResultCallback;
  13. import com.github.dockerjava.httpclient5.ApacheDockerHttpClient;
  14. import com.github.dockerjava.transport.DockerHttpClient;
  15. import com.miniframe.solverjob.DockerExe;
  16. import java.io.Closeable;
  17. import java.io.IOException;
  18. import java.util.ArrayList;
  19. import java.util.List;
  20. import static com.github.dockerjava.api.model.MountType.VOLUME;
  21. public class DockerTest {
  22. public static final String DOCKERHOST="tcp://192.168.0.132:2375/";
  23. public static final String APIVERSION="1.13.1";
  24. public static void main(String[] args) throws InterruptedException {
  25. // 初始化 Docker 客户端
  26. // System.out.println("容器已成功停止: " + getDocker(7182031)); ;
  27. DockerExe.stopDocker(982,"Water");
  28. }
  29. public static String getDocker(Integer aid){
  30. DockerClientConfig config = DefaultDockerClientConfig.createDefaultConfigBuilder()
  31. .withDockerHost(DOCKERHOST) // 设置 Docker 主机地址
  32. .withDockerTlsVerify(false) // 启用 TLS 验证
  33. .withApiVersion(APIVERSION) // 设置 API 版本
  34. .build();
  35. DockerHttpClient httpClient = new ApacheDockerHttpClient.Builder()
  36. .dockerHost(config.getDockerHost())
  37. .sslConfig(config.getSSLConfig())
  38. .build();
  39. DockerClient dockerClient = DockerClientBuilder.getInstance(config)
  40. .withDockerHttpClient(httpClient)
  41. .build();
  42. InspectContainerResponse containerInfo = dockerClient.inspectContainerCmd(aid.toString()).exec();
  43. return containerInfo.getState().getStatus();
  44. // if (containerInfo.getState().equals("exited")) {
  45. // System.out.println("容器已成功停止: " + aid.toString());
  46. // } else {
  47. // System.out.println("容器状态: " + containerInfo.getState());
  48. // }
  49. }
  50. private static void fireExec() {
  51. DockerClientConfig config = DefaultDockerClientConfig.createDefaultConfigBuilder()
  52. .withDockerHost("tcp://192.168.0.132:2375/") // 设置 Docker 主机地址
  53. .withDockerTlsVerify(false) // 启用 TLS 验证
  54. .withApiVersion("1.13.1") // 设置 API 版本
  55. .build();
  56. DockerHttpClient httpClient = new ApacheDockerHttpClient.Builder()
  57. .dockerHost(config.getDockerHost())
  58. .sslConfig(config.getSSLConfig())
  59. .build();
  60. DockerClient dockerClient = DockerClientBuilder.getInstance(config)
  61. .withDockerHttpClient(httpClient)
  62. .build();
  63. Mount zhtyVMount = new Mount()
  64. .withType(MountType.BIND)
  65. .withSource("/home/disaster/zhty/")
  66. .withTarget("/home/disaster/zhty/")
  67. .withReadOnly(false);
  68. Mount cephfsVMount = new Mount()
  69. .withType(MountType.BIND)
  70. .withSource("/cephfs/disaster/")
  71. .withTarget("/cephfs/disaster/")
  72. .withReadOnly(false);
  73. Mount runWaterMount = new Mount()
  74. .withType(MountType.BIND)
  75. .withSource("/cephfs/disaster/6/2054/fire/runFile.sh")
  76. .withTarget("/home/disaster/zhty/Fire/runFire.sh")
  77. .withReadOnly(false);
  78. List<Mount> am =new ArrayList<>();
  79. am.add(zhtyVMount);
  80. am.add(cephfsVMount);
  81. am.add(runWaterMount);
  82. //创建容器
  83. CreateContainerResponse container = dockerClient
  84. .createContainerCmd("fire:1.0")//镜像名称
  85. .withName("718201")//容器名称
  86. .withHostConfig(
  87. HostConfig.newHostConfig()
  88. .withMounts(am)
  89. ).exec();
  90. dockerClient.startContainerCmd(container.getId()).exec();
  91. System.out.println("容器启动成功,ID: " + container.getId());
  92. }
  93. private static void waterExec() throws InterruptedException {
  94. DockerClientConfig config = DefaultDockerClientConfig.createDefaultConfigBuilder()
  95. .withDockerHost("tcp://192.168.0.132:2375/") // 设置 Docker 主机地址
  96. .withDockerTlsVerify(false) // 启用 TLS 验证
  97. .withApiVersion("1.13.1") // 设置 API 版本
  98. .build();
  99. DockerHttpClient httpClient = new ApacheDockerHttpClient.Builder()
  100. .dockerHost(config.getDockerHost())
  101. .sslConfig(config.getSSLConfig())
  102. .build();
  103. DockerClient dockerClient = DockerClientBuilder.getInstance(config)
  104. .withDockerHttpClient(httpClient)
  105. .build();
  106. Mount zhtyVMount = new Mount()
  107. .withType(MountType.BIND)
  108. .withSource("/home/disaster/zhty/")
  109. .withTarget("/home/disaster/zhty/")
  110. .withReadOnly(false);
  111. Mount cephfsVMount = new Mount()
  112. .withType(MountType.BIND)
  113. .withSource("/cephfs/disaster/")
  114. .withTarget("/cephfs/disaster/")
  115. .withReadOnly(false);
  116. Mount runWaterMount = new Mount()
  117. .withType(MountType.BIND)
  118. .withSource("/cephfs/disaster/7/1820/water/runWater.sh")
  119. .withTarget("/home/disaster/zhty/Water/runWater.sh")
  120. .withReadOnly(false);
  121. List<Mount> am =new ArrayList<>();
  122. am.add(zhtyVMount);
  123. am.add(cephfsVMount);
  124. am.add(runWaterMount);
  125. //创建容器
  126. CreateContainerResponse container = dockerClient
  127. .createContainerCmd("water:1.0")//镜像名称
  128. .withName("718203")//容器名称
  129. .withHostConfig(
  130. HostConfig.newHostConfig()
  131. .withMounts(am)
  132. ).exec();
  133. dockerClient.startContainerCmd(container.getId()).exec();
  134. System.out.println("容器启动成功,ID: " + container.getId());
  135. // 获取容器日志
  136. LogContainerCmd logContainerCmd = dockerClient.logContainerCmd(container.getId())
  137. .withStdOut(true)
  138. .withStdErr(true)
  139. .withFollowStream(true);
  140. // 使用 ResultCallback.Adapter 处理日志
  141. logContainerCmd.exec(new ResultCallback.Adapter<Frame>() {
  142. @Override
  143. public void onNext(Frame frame) {
  144. System.out.print(new String(frame.getPayload()));
  145. super.onNext(frame);
  146. }
  147. @Override
  148. public void onError(Throwable throwable) {
  149. System.err.println("日志获取失败");
  150. throwable.printStackTrace();
  151. super.onError(throwable);
  152. }
  153. @Override
  154. public void onComplete() {
  155. System.out.println("日志获取完成");
  156. super.onComplete();
  157. }
  158. }).awaitCompletion();
  159. }
  160. private static void getLogs() throws InterruptedException {
  161. DockerClientConfig config = DefaultDockerClientConfig.createDefaultConfigBuilder()
  162. .withDockerHost("tcp://192.168.0.132:2375/") // 设置 Docker 主机地址
  163. .withDockerTlsVerify(false) // 启用 TLS 验证
  164. .withApiVersion("1.13.1") // 设置 API 版本
  165. .build();
  166. DockerHttpClient httpClient = new ApacheDockerHttpClient.Builder()
  167. .dockerHost(config.getDockerHost())
  168. .sslConfig(config.getSSLConfig())
  169. .build();
  170. DockerClient dockerClient = DockerClientBuilder.getInstance(config)
  171. .withDockerHttpClient(httpClient)
  172. .build();
  173. // 获取容器日志
  174. LogContainerCmd logContainerCmd = dockerClient.logContainerCmd("718203")
  175. .withStdOut(true)
  176. .withStdErr(true)
  177. .withFollowStream(true);
  178. // 使用 ResultCallback.Adapter 处理日志
  179. logContainerCmd.exec(new ResultCallback.Adapter<Frame>() {
  180. @Override
  181. public void onNext(Frame frame) {
  182. System.out.print(new String(frame.getPayload()));
  183. super.onNext(frame);
  184. }
  185. @Override
  186. public void onError(Throwable throwable) {
  187. System.err.println("日志获取失败");
  188. throwable.printStackTrace();
  189. super.onError(throwable);
  190. }
  191. @Override
  192. public void onComplete() {
  193. System.out.println("日志获取完成");
  194. super.onComplete();
  195. }
  196. }).awaitCompletion();
  197. System.out.println("1111日志获取完成");
  198. }
  199. }