DockerExe.java 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. package com.miniframe.tools.docker;
  2. import com.github.dockerjava.api.DockerClient;
  3. import com.github.dockerjava.api.async.ResultCallback;
  4. import com.github.dockerjava.api.command.*;
  5. import com.github.dockerjava.api.model.Frame;
  6. import com.github.dockerjava.api.model.HostConfig;
  7. import com.github.dockerjava.api.model.Mount;
  8. import com.github.dockerjava.api.model.MountType;
  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.httpclient5.ApacheDockerHttpClient;
  13. import com.github.dockerjava.transport.DockerHttpClient;
  14. import java.util.ArrayList;
  15. import java.util.List;
  16. public class DockerExe {
  17. // public static String DOCKERHOST="tcp://127.0.0.1:2375/";
  18. // public static String APIVERSION="26.1.0";
  19. public static final String DOCKERHOST="tcp://192.168.0.132:2375/";
  20. public static final String APIVERSION="1.13.1";
  21. // 获取容器信息
  22. public static String getDocker(String pid){
  23. DockerClientConfig config = DefaultDockerClientConfig.createDefaultConfigBuilder()
  24. .withDockerHost(DOCKERHOST) // 设置 Docker 主机地址
  25. .withDockerTlsVerify(false) // 启用 TLS 验证
  26. .withApiVersion(APIVERSION) // 设置 API 版本
  27. .build();
  28. DockerHttpClient httpClient = new ApacheDockerHttpClient.Builder()
  29. .dockerHost(config.getDockerHost())
  30. .sslConfig(config.getSSLConfig())
  31. .build();
  32. DockerClient dockerClient = DockerClientBuilder.getInstance(config)
  33. .withDockerHttpClient(httpClient)
  34. .build();
  35. InspectContainerResponse containerInfo = dockerClient.inspectContainerCmd(pid).exec();
  36. //exited 停止 running 运行
  37. try{
  38. return containerInfo.getState().getStatus();
  39. }catch (Exception e){
  40. return "No such container:"+pid;
  41. }
  42. }
  43. public static void stopDocker(String pid){
  44. DockerClientConfig config = DefaultDockerClientConfig.createDefaultConfigBuilder()
  45. .withDockerHost(DOCKERHOST) // 设置 Docker 主机地址
  46. .withDockerTlsVerify(false) // 启用 TLS 验证
  47. .withApiVersion(APIVERSION) // 设置 API 版本
  48. .build();
  49. DockerHttpClient httpClient = new ApacheDockerHttpClient.Builder()
  50. .dockerHost(config.getDockerHost())
  51. .sslConfig(config.getSSLConfig())
  52. .build();
  53. DockerClient dockerClient = DockerClientBuilder.getInstance(config)
  54. .withDockerHttpClient(httpClient)
  55. .build();
  56. try {
  57. RemoveContainerCmd removeContainerCmd = dockerClient.removeContainerCmd(pid)
  58. .withForce(true);
  59. removeContainerCmd.exec();
  60. System.out.println("容器已停止: " + pid);
  61. } catch (Exception e) {
  62. System.err.println("停止容器失败: " + e.getMessage());
  63. // e.printStackTrace();
  64. }
  65. }
  66. public static void getDockerLogs(String pid, ResultCallback.Adapter<Frame> logsexe) throws InterruptedException {
  67. DockerClientConfig config = DefaultDockerClientConfig.createDefaultConfigBuilder()
  68. .withDockerHost(DOCKERHOST) // 设置 Docker 主机地址
  69. .withDockerTlsVerify(false) // 启用 TLS 验证
  70. .withApiVersion(APIVERSION) // 设置 API 版本
  71. .build();
  72. DockerHttpClient httpClient = new ApacheDockerHttpClient.Builder()
  73. .dockerHost(config.getDockerHost())
  74. .sslConfig(config.getSSLConfig())
  75. .build();
  76. DockerClient dockerClient = DockerClientBuilder.getInstance(config)
  77. .withDockerHttpClient(httpClient)
  78. .build();
  79. // 获取容器日志
  80. LogContainerCmd logContainerCmd = dockerClient.logContainerCmd(pid)
  81. .withStdOut(true)
  82. .withStdErr(true)
  83. .withFollowStream(true);
  84. // 使用 ResultCallback.Adapter 处理日志
  85. logContainerCmd.exec(logsexe).awaitCompletion();
  86. }
  87. public static void runMdo(String pid) {
  88. DockerClientConfig config = DefaultDockerClientConfig.createDefaultConfigBuilder()
  89. .withDockerHost(DOCKERHOST) // 设置 Docker 主机地址
  90. .withDockerTlsVerify(false) // 启用 TLS 验证
  91. .withApiVersion(APIVERSION) // 设置 API 版本
  92. .build();
  93. DockerHttpClient httpClient = new ApacheDockerHttpClient.Builder()
  94. .dockerHost(config.getDockerHost())
  95. .sslConfig(config.getSSLConfig())
  96. .build();
  97. DockerClient dockerClient = DockerClientBuilder.getInstance(config)
  98. .withDockerHttpClient(httpClient)
  99. .build();
  100. Mount cephfsVMount = new Mount()
  101. .withType(MountType.BIND)
  102. .withSource("/cephfs/mdo/")
  103. .withTarget("/cephfs/mdo/")
  104. .withReadOnly(false);
  105. Mount sitePackages = new Mount()
  106. .withType(MountType.BIND)
  107. .withSource("/home/xgd/site-packages/")
  108. .withTarget("/opt/miniconda3/envs/surromdao/lib/python3.10/site-packages/")
  109. .withReadOnly(false);
  110. Mount outMount = new Mount()
  111. .withType(MountType.BIND)
  112. .withSource("/cephfs/mdo/"+pid +"/out/")
  113. .withTarget("/workspace/outdata/")
  114. .withReadOnly(false);
  115. Mount runMount = new Mount()
  116. .withType(MountType.BIND)
  117. .withSource("/cephfs/mdo/"+pid +"/in/run.py")
  118. .withTarget("/workspace/run.py")
  119. .withReadOnly(false);
  120. Mount nsga2_history1 = new Mount()
  121. .withType(MountType.BIND)
  122. .withSource("/cephfs/mdo/"+pid +"/out/nsga2_history1.txt")
  123. .withTarget("/workspace/nsga2_history1.txt")
  124. .withReadOnly(false);
  125. Mount nsga2_history2 = new Mount()
  126. .withType(MountType.BIND)
  127. .withSource("/cephfs/mdo/"+pid +"/out/nsga2_history2.txt")
  128. .withTarget("/workspace/nsga2_history2.txt")
  129. .withReadOnly(false);
  130. List<Mount> am =new ArrayList<>();
  131. am.add(cephfsVMount);
  132. am.add(sitePackages);
  133. am.add(outMount);
  134. am.add(runMount);
  135. am.add(nsga2_history1);
  136. am.add(nsga2_history2);
  137. //创建容器
  138. CreateContainerResponse container = dockerClient
  139. .createContainerCmd("xgd:2.0")//镜像名称
  140. .withName(pid)//容器名称
  141. .withHostConfig(
  142. HostConfig.newHostConfig()
  143. .withMounts(am)
  144. ).exec();
  145. dockerClient.startContainerCmd(container.getId()).exec();
  146. System.out.println("容器启动成功,ID: " + container.getId());
  147. }
  148. public static void runMdo2(String pid) {
  149. DockerClientConfig config = DefaultDockerClientConfig.createDefaultConfigBuilder()
  150. .withDockerHost(DOCKERHOST) // 设置 Docker 主机地址
  151. .withDockerTlsVerify(false) // 启用 TLS 验证
  152. .withApiVersion(APIVERSION) // 设置 API 版本
  153. .build();
  154. DockerHttpClient httpClient = new ApacheDockerHttpClient.Builder()
  155. .dockerHost(config.getDockerHost())
  156. .sslConfig(config.getSSLConfig())
  157. .build();
  158. DockerClient dockerClient = DockerClientBuilder.getInstance(config)
  159. .withDockerHttpClient(httpClient)
  160. .build();
  161. Mount wokerMount = new Mount()
  162. .withType(MountType.BIND)
  163. .withSource("/cephfs/mdo/"+pid+"/in/")
  164. .withTarget("/cephfs/mdo/"+pid+"/in/")
  165. .withReadOnly(false);
  166. List<Mount> am =new ArrayList<>();
  167. am.add(wokerMount);
  168. //创建容器
  169. CreateContainerResponse container = dockerClient
  170. .createContainerCmd("surromdao:test")//镜像名称
  171. .withName(pid)//容器名称
  172. .withHostConfig(
  173. HostConfig.newHostConfig()
  174. .withMounts(am)
  175. ).withCmd("bash", "-c", "source /home/mdo/aeroelastic.sh && cd /cephfs/mdo/"+pid+"/in && mpiexec -n 1 python run.py")
  176. .exec();
  177. dockerClient.startContainerCmd(container.getId()).exec();
  178. System.out.println("容器启动成功,ID: " + container.getId());
  179. }
  180. public static void main(String[] args) {
  181. stopDocker("6cb37eea2845457ca17ba6441804af43");
  182. runMdo2("6cb37eea2845457ca17ba6441804af43");
  183. }
  184. }