package com.miniframe.tools.docker;
import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.async.ResultCallback;
import com.github.dockerjava.api.command.*;
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(String pid){
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(pid).exec();
//exited 停止 running 运行
try{
return containerInfo.getState().getStatus();
}catch (Exception e){
return "No such container:"+pid;
}
}
public static void stopDocker(String pid){
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 {
RemoveContainerCmd removeContainerCmd = dockerClient.removeContainerCmd(pid)
.withForce(true);
removeContainerCmd.exec();
System.out.println("容器已停止: " + pid);
} catch (Exception e) {
System.err.println("停止容器失败: " + e.getMessage());
// e.printStackTrace();
}
}
public static void getDockerLogs(String pid, 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(pid)
.withStdOut(true)
.withStdErr(true)
.withFollowStream(true);
// 使用 ResultCallback.Adapter 处理日志
logContainerCmd.exec(logsexe).awaitCompletion();
}
public static void runMdo(String pid) {
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 cephfsVMount = new Mount()
.withType(MountType.BIND)
.withSource("/cephfs/mdo/")
.withTarget("/cephfs/mdo/")
.withReadOnly(false);
Mount sitePackages = new Mount()
.withType(MountType.BIND)
.withSource("/home/xgd/site-packages/")
.withTarget("/opt/miniconda3/envs/surromdao/lib/python3.10/site-packages/")
.withReadOnly(false);
Mount outMount = new Mount()
.withType(MountType.BIND)
.withSource("/cephfs/mdo/"+pid +"/out/")
.withTarget("/workspace/outdata/")
.withReadOnly(false);
Mount runMount = new Mount()
.withType(MountType.BIND)
.withSource("/cephfs/mdo/"+pid +"/in/run.py")
.withTarget("/workspace/run.py")
.withReadOnly(false);
Mount nsga2_history1 = new Mount()
.withType(MountType.BIND)
.withSource("/cephfs/mdo/"+pid +"/out/nsga2_history1.txt")
.withTarget("/workspace/nsga2_history1.txt")
.withReadOnly(false);
Mount nsga2_history2 = new Mount()
.withType(MountType.BIND)
.withSource("/cephfs/mdo/"+pid +"/out/nsga2_history2.txt")
.withTarget("/workspace/nsga2_history2.txt")
.withReadOnly(false);
List am =new ArrayList<>();
am.add(cephfsVMount);
am.add(sitePackages);
am.add(outMount);
am.add(runMount);
am.add(nsga2_history1);
am.add(nsga2_history2);
//创建容器
CreateContainerResponse container = dockerClient
.createContainerCmd("xgd:2.0")//镜像名称
.withName(pid)//容器名称
.withHostConfig(
HostConfig.newHostConfig()
.withMounts(am)
).exec();
dockerClient.startContainerCmd(container.getId()).exec();
System.out.println("容器启动成功,ID: " + container.getId());
}
public static void runMdo2(String pid) {
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 wokerMount = new Mount()
.withType(MountType.BIND)
.withSource("/cephfs/mdo/"+pid+"/in/")
.withTarget("/cephfs/mdo/"+pid+"/in/")
.withReadOnly(false);
List am =new ArrayList<>();
am.add(wokerMount);
//创建容器
CreateContainerResponse container = dockerClient
.createContainerCmd("surromdao:test")//镜像名称
.withName(pid)//容器名称
.withHostConfig(
HostConfig.newHostConfig()
.withMounts(am)
).withCmd("bash", "-c", "source /home/mdo/aeroelastic.sh && cd /cephfs/mdo/"+pid+"/in && mpiexec -n 1 python run.py")
.exec();
dockerClient.startContainerCmd(container.getId()).exec();
System.out.println("容器启动成功,ID: " + container.getId());
}
public static void main(String[] args) {
stopDocker("6cb37eea2845457ca17ba6441804af43");
runMdo2("6cb37eea2845457ca17ba6441804af43");
}
}