|
@@ -5,10 +5,12 @@ 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.*;
|
|
|
import com.github.dockerjava.core.DefaultDockerClientConfig;
|
|
|
import com.github.dockerjava.core.DockerClientBuilder;
|
|
|
import com.github.dockerjava.core.DockerClientConfig;
|
|
|
+import com.github.dockerjava.core.command.RemoveContainerCmdImpl;
|
|
|
import com.github.dockerjava.httpclient5.ApacheDockerHttpClient;
|
|
|
import com.github.dockerjava.transport.DockerHttpClient;
|
|
|
import com.miniframe.disaster.service.LogService;
|
|
@@ -58,11 +60,25 @@ public class DockerExe {
|
|
|
.withDockerHttpClient(httpClient)
|
|
|
.build();
|
|
|
try {
|
|
|
- dockerClient.stopContainerCmd(stype+"_"+aid.toString())
|
|
|
- .withTimeout(1000)
|
|
|
- .exec();
|
|
|
- dockerClient.removeConfigCmd(aid.toString())
|
|
|
- .exec();
|
|
|
+ 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());
|