|
@@ -0,0 +1,212 @@
|
|
|
+package com.miniframe;
|
|
|
+
|
|
|
+import com.github.dockerjava.api.DockerClient;
|
|
|
+import com.github.dockerjava.api.async.ResultCallback;
|
|
|
+import com.github.dockerjava.api.command.CreateContainerResponse;
|
|
|
+import com.github.dockerjava.api.command.InspectContainerResponse;
|
|
|
+import com.github.dockerjava.api.command.LogContainerCmd;
|
|
|
+import com.github.dockerjava.api.command.PullImageResultCallback;
|
|
|
+import com.github.dockerjava.api.model.*;
|
|
|
+import com.github.dockerjava.core.DefaultDockerClientConfig;
|
|
|
+import com.github.dockerjava.core.DockerClientBuilder;
|
|
|
+import com.github.dockerjava.core.DockerClientConfig;
|
|
|
+import com.github.dockerjava.core.command.LogContainerResultCallback;
|
|
|
+import com.github.dockerjava.httpclient5.ApacheDockerHttpClient;
|
|
|
+import com.github.dockerjava.transport.DockerHttpClient;
|
|
|
+
|
|
|
+import java.io.Closeable;
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+import static com.github.dockerjava.api.model.MountType.VOLUME;
|
|
|
+
|
|
|
+public class DockerTest {
|
|
|
+ public static final String DOCKERHOST="tcp://192.168.0.132:2375/";
|
|
|
+ public static final String APIVERSION="1.13.1";
|
|
|
+ public static void main(String[] args) throws InterruptedException {
|
|
|
+ // 初始化 Docker 客户端
|
|
|
+// System.out.println("容器已成功停止: " + getDocker(7182031)); ;
|
|
|
+ getLogs();
|
|
|
+ }
|
|
|
+ public static String getDocker(Integer aid){
|
|
|
+ DockerClientConfig config = DefaultDockerClientConfig.createDefaultConfigBuilder()
|
|
|
+ .withDockerHost(DOCKERHOST) // 设置 Docker 主机地址
|
|
|
+ .withDockerTlsVerify(false) // 启用 TLS 验证
|
|
|
+ .withApiVersion(APIVERSION) // 设置 API 版本
|
|
|
+ .build();
|
|
|
+ DockerHttpClient httpClient = new ApacheDockerHttpClient.Builder()
|
|
|
+ .dockerHost(config.getDockerHost())
|
|
|
+ .sslConfig(config.getSSLConfig())
|
|
|
+ .build();
|
|
|
+ DockerClient dockerClient = DockerClientBuilder.getInstance(config)
|
|
|
+ .withDockerHttpClient(httpClient)
|
|
|
+ .build();
|
|
|
+ InspectContainerResponse containerInfo = dockerClient.inspectContainerCmd(aid.toString()).exec();
|
|
|
+ return containerInfo.getState().getStatus();
|
|
|
+// if (containerInfo.getState().equals("exited")) {
|
|
|
+// System.out.println("容器已成功停止: " + aid.toString());
|
|
|
+// } else {
|
|
|
+// System.out.println("容器状态: " + containerInfo.getState());
|
|
|
+// }
|
|
|
+ }
|
|
|
+ private static void fireExec() {
|
|
|
+ DockerClientConfig config = DefaultDockerClientConfig.createDefaultConfigBuilder()
|
|
|
+ .withDockerHost("tcp://192.168.0.132:2375/") // 设置 Docker 主机地址
|
|
|
+ .withDockerTlsVerify(false) // 启用 TLS 验证
|
|
|
+ .withApiVersion("1.13.1") // 设置 API 版本
|
|
|
+ .build();
|
|
|
+ DockerHttpClient httpClient = new ApacheDockerHttpClient.Builder()
|
|
|
+ .dockerHost(config.getDockerHost())
|
|
|
+ .sslConfig(config.getSSLConfig())
|
|
|
+ .build();
|
|
|
+ DockerClient dockerClient = DockerClientBuilder.getInstance(config)
|
|
|
+ .withDockerHttpClient(httpClient)
|
|
|
+ .build();
|
|
|
+ Mount zhtyVMount = new Mount()
|
|
|
+ .withType(MountType.BIND)
|
|
|
+ .withSource("/home/disaster/zhty/")
|
|
|
+ .withTarget("/home/disaster/zhty/")
|
|
|
+ .withReadOnly(false);
|
|
|
+ Mount cephfsVMount = new Mount()
|
|
|
+ .withType(MountType.BIND)
|
|
|
+ .withSource("/cephfs/disaster/")
|
|
|
+ .withTarget("/cephfs/disaster/")
|
|
|
+ .withReadOnly(false);
|
|
|
+ Mount runWaterMount = new Mount()
|
|
|
+ .withType(MountType.BIND)
|
|
|
+ .withSource("/cephfs/disaster/6/2054/fire/runFile.sh")
|
|
|
+ .withTarget("/home/disaster/zhty/Fire/runFire.sh")
|
|
|
+ .withReadOnly(false);
|
|
|
+ List<Mount> am =new ArrayList<>();
|
|
|
+ am.add(zhtyVMount);
|
|
|
+ am.add(cephfsVMount);
|
|
|
+ am.add(runWaterMount);
|
|
|
+ //创建容器
|
|
|
+ CreateContainerResponse container = dockerClient
|
|
|
+ .createContainerCmd("fire:1.0")//镜像名称
|
|
|
+ .withName("718201")//容器名称
|
|
|
+ .withHostConfig(
|
|
|
+ HostConfig.newHostConfig()
|
|
|
+ .withMounts(am)
|
|
|
+ ).exec();
|
|
|
+ dockerClient.startContainerCmd(container.getId()).exec();
|
|
|
+ System.out.println("容器启动成功,ID: " + container.getId());
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void waterExec() throws InterruptedException {
|
|
|
+ DockerClientConfig config = DefaultDockerClientConfig.createDefaultConfigBuilder()
|
|
|
+ .withDockerHost("tcp://192.168.0.132:2375/") // 设置 Docker 主机地址
|
|
|
+ .withDockerTlsVerify(false) // 启用 TLS 验证
|
|
|
+ .withApiVersion("1.13.1") // 设置 API 版本
|
|
|
+ .build();
|
|
|
+ DockerHttpClient httpClient = new ApacheDockerHttpClient.Builder()
|
|
|
+ .dockerHost(config.getDockerHost())
|
|
|
+ .sslConfig(config.getSSLConfig())
|
|
|
+ .build();
|
|
|
+ DockerClient dockerClient = DockerClientBuilder.getInstance(config)
|
|
|
+ .withDockerHttpClient(httpClient)
|
|
|
+ .build();
|
|
|
+ Mount zhtyVMount = new Mount()
|
|
|
+ .withType(MountType.BIND)
|
|
|
+ .withSource("/home/disaster/zhty/")
|
|
|
+ .withTarget("/home/disaster/zhty/")
|
|
|
+ .withReadOnly(false);
|
|
|
+ Mount cephfsVMount = new Mount()
|
|
|
+ .withType(MountType.BIND)
|
|
|
+ .withSource("/cephfs/disaster/")
|
|
|
+ .withTarget("/cephfs/disaster/")
|
|
|
+ .withReadOnly(false);
|
|
|
+ Mount runWaterMount = new Mount()
|
|
|
+ .withType(MountType.BIND)
|
|
|
+ .withSource("/cephfs/disaster/7/1820/water/runWater.sh")
|
|
|
+ .withTarget("/home/disaster/zhty/Water/runWater.sh")
|
|
|
+ .withReadOnly(false);
|
|
|
+ List<Mount> am =new ArrayList<>();
|
|
|
+ am.add(zhtyVMount);
|
|
|
+ am.add(cephfsVMount);
|
|
|
+ am.add(runWaterMount);
|
|
|
+ //创建容器
|
|
|
+ CreateContainerResponse container = dockerClient
|
|
|
+ .createContainerCmd("water:1.0")//镜像名称
|
|
|
+ .withName("718203")//容器名称
|
|
|
+ .withHostConfig(
|
|
|
+ HostConfig.newHostConfig()
|
|
|
+ .withMounts(am)
|
|
|
+ ).exec();
|
|
|
+ dockerClient.startContainerCmd(container.getId()).exec();
|
|
|
+ System.out.println("容器启动成功,ID: " + container.getId());
|
|
|
+
|
|
|
+ // 获取容器日志
|
|
|
+ LogContainerCmd logContainerCmd = dockerClient.logContainerCmd(container.getId())
|
|
|
+ .withStdOut(true)
|
|
|
+ .withStdErr(true)
|
|
|
+ .withFollowStream(true);
|
|
|
+
|
|
|
+ // 使用 ResultCallback.Adapter 处理日志
|
|
|
+ logContainerCmd.exec(new ResultCallback.Adapter<Frame>() {
|
|
|
+ @Override
|
|
|
+ public void onNext(Frame frame) {
|
|
|
+ System.out.print(new String(frame.getPayload()));
|
|
|
+ super.onNext(frame);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onError(Throwable throwable) {
|
|
|
+ System.err.println("日志获取失败");
|
|
|
+ throwable.printStackTrace();
|
|
|
+ super.onError(throwable);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onComplete() {
|
|
|
+ System.out.println("日志获取完成");
|
|
|
+ super.onComplete();
|
|
|
+ }
|
|
|
+ }).awaitCompletion();
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void getLogs() throws InterruptedException {
|
|
|
+ DockerClientConfig config = DefaultDockerClientConfig.createDefaultConfigBuilder()
|
|
|
+ .withDockerHost("tcp://192.168.0.132:2375/") // 设置 Docker 主机地址
|
|
|
+ .withDockerTlsVerify(false) // 启用 TLS 验证
|
|
|
+ .withApiVersion("1.13.1") // 设置 API 版本
|
|
|
+ .build();
|
|
|
+ DockerHttpClient httpClient = new ApacheDockerHttpClient.Builder()
|
|
|
+ .dockerHost(config.getDockerHost())
|
|
|
+ .sslConfig(config.getSSLConfig())
|
|
|
+ .build();
|
|
|
+ DockerClient dockerClient = DockerClientBuilder.getInstance(config)
|
|
|
+ .withDockerHttpClient(httpClient)
|
|
|
+ .build();
|
|
|
+ // 获取容器日志
|
|
|
+ LogContainerCmd logContainerCmd = dockerClient.logContainerCmd("718203")
|
|
|
+ .withStdOut(true)
|
|
|
+ .withStdErr(true)
|
|
|
+ .withFollowStream(true);
|
|
|
+
|
|
|
+ // 使用 ResultCallback.Adapter 处理日志
|
|
|
+ logContainerCmd.exec(new ResultCallback.Adapter<Frame>() {
|
|
|
+ @Override
|
|
|
+ public void onNext(Frame frame) {
|
|
|
+ System.out.print(new String(frame.getPayload()));
|
|
|
+ super.onNext(frame);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onError(Throwable throwable) {
|
|
|
+ System.err.println("日志获取失败");
|
|
|
+ throwable.printStackTrace();
|
|
|
+ super.onError(throwable);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onComplete() {
|
|
|
+ System.out.println("日志获取完成");
|
|
|
+ super.onComplete();
|
|
|
+ }
|
|
|
+ }).awaitCompletion();
|
|
|
+ System.out.println("1111日志获取完成");
|
|
|
+ }
|
|
|
+
|
|
|
+}
|