package com.miniframe.solverjob; 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.RemoveContainerCmd; import com.github.dockerjava.api.model.Frame; import com.github.dockerjava.api.model.HostConfig; import com.github.dockerjava.api.model.Mount; import com.github.dockerjava.api.model.MountType; import com.github.dockerjava.core.DefaultDockerClientConfig; import com.github.dockerjava.core.DockerClientBuilder; import com.github.dockerjava.core.DockerClientConfig; import com.github.dockerjava.httpclient5.ApacheDockerHttpClient; import com.github.dockerjava.transport.DockerHttpClient; import java.util.ArrayList; import java.util.List; public class DockerExe { // public static String DOCKERHOST="tcp://127.0.0.1:2375/"; // public static String APIVERSION="26.1.0"; public static final String DOCKERHOST="tcp://192.168.0.132:2375/"; public static final String APIVERSION="1.13.1"; // 获取容器信息 public static String getDocker(Integer aid,String stype){ 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(stype+"_"+aid.toString()).exec(); //exited 停止 running 运行 try{ return containerInfo.getState().getStatus(); }catch (Exception e){ return "No such container:"+aid; } } public static void stopDocker(Integer aid, String stype){ 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(); try { // InspectContainerResponse containerInfo = dockerClient.inspectContainerCmd(stype+"_"+aid.toString()).exec(); // //exited 停止 running 运行 // String status ="error"; // try{ // status= containerInfo.getState().getStatus(); // }catch (Exception e){ // // } // if("running".equals(status)){ // dockerClient.stopContainerCmd(stype+"_"+aid.toString()) // .withTimeout(1000) // .exec(); // } // if("exited".equals(status)){ RemoveContainerCmd removeContainerCmd = dockerClient.removeContainerCmd(stype+"_"+aid.toString()) .withForce(true); removeContainerCmd.exec(); // } System.out.println("容器已停止: " + aid.toString()); } catch (Exception e) { System.err.println("停止容器失败: " + e.getMessage()); e.printStackTrace(); } } public static void getDockerLogs(Integer aid,String stype, ResultCallback.Adapter logsexe) throws InterruptedException { 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(); // 获取容器日志 LogContainerCmd logContainerCmd = dockerClient.logContainerCmd(stype+"_"+aid.toString()) .withStdOut(true) .withStdErr(true) .withFollowStream(true); // 使用 ResultCallback.Adapter 处理日志 logContainerCmd.exec(logsexe).awaitCompletion(); } public static void fireGass(Integer aid, Integer jid,String stype) { 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(); 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 gasBinMount = new Mount() .withType(MountType.BIND) .withSource("/home/disaster/gas/bin/") .withTarget("/home/disaster/gas/bin/") .withReadOnly(false); Mount runGasMount = new Mount() .withType(MountType.BIND) .withSource("/cephfs/disaster/" + aid + "/" + jid + "/gas/run.sh") .withTarget("/home/disaster/zhty/Gas/runGas.sh") .withReadOnly(false); List am =new ArrayList<>(); am.add(zhtyVMount); am.add(cephfsVMount); am.add(gasBinMount); am.add(runGasMount); //创建容器 CreateContainerResponse container = dockerClient .createContainerCmd("gass:1.0")//镜像名称 .withName(stype+"_"+aid)//容器名称 .withHostConfig( HostConfig.newHostConfig() .withMounts(am) ).exec(); dockerClient.startContainerCmd(container.getId()).exec(); System.out.println("容器启动成功,ID: " + container.getId()); } public static void fireExec(Integer aid, Integer jid,String stype) { 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(); 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/" + aid + "/" + jid + "/fire/runFile.sh") .withTarget("/home/disaster/zhty/Fire/runFire.sh") .withReadOnly(false); List am =new ArrayList<>(); am.add(zhtyVMount); am.add(cephfsVMount); am.add(runWaterMount); //创建容器 CreateContainerResponse container = dockerClient .createContainerCmd("fire:1.0")//镜像名称 .withName(stype+"_"+aid)//容器名称 .withHostConfig( HostConfig.newHostConfig() .withMounts(am) ).exec(); dockerClient.startContainerCmd(container.getId()).exec(); System.out.println("容器启动成功,ID: " + container.getId()); } public static void waterExec(Integer aid, Integer jid,String stype) throws InterruptedException { 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(); 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/" + aid + "/" + jid + "/water/runWater.sh") .withTarget("/home/disaster/zhty/Water/runWater.sh") .withReadOnly(false); List am =new ArrayList<>(); am.add(zhtyVMount); am.add(cephfsVMount); am.add(runWaterMount); //创建容器 CreateContainerResponse container = dockerClient .createContainerCmd("water:1.0")//镜像名称 .withName(stype+"_"+aid)//容器名称 .withHostConfig( HostConfig.newHostConfig() .withMounts(am) ).exec(); dockerClient.startContainerCmd(container.getId()).exec(); System.out.println("容器启动成功,ID: " + container.getId()); } }