|
@@ -18,8 +18,10 @@ import com.github.dockerjava.transport.DockerHttpClient;
|
|
|
import java.io.File;
|
|
import java.io.File;
|
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
|
import java.time.Duration;
|
|
import java.time.Duration;
|
|
|
|
|
+import java.util.Arrays;
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
import java.util.Set;
|
|
import java.util.Set;
|
|
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* Docker 客户端
|
|
* Docker 客户端
|
|
@@ -136,19 +138,19 @@ public class DockerImpl implements Docker {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
- public String getContainerIdByName(String containerName) throws DockerException {
|
|
|
|
|
|
|
+ public String getContainerIdByName(String containerName) {
|
|
|
List<Container> list = dockerClient.listContainersCmd()
|
|
List<Container> list = dockerClient.listContainersCmd()
|
|
|
.withShowAll(true)
|
|
.withShowAll(true)
|
|
|
.withNameFilter(List.of(containerName))
|
|
.withNameFilter(List.of(containerName))
|
|
|
.exec();
|
|
.exec();
|
|
|
- if (list.size() > 1) {
|
|
|
|
|
- String msg = String.format("有多个名字为 %s 的容器", containerName);
|
|
|
|
|
- throw new DockerException(msg);
|
|
|
|
|
- } else if (list.size() == 1) {
|
|
|
|
|
- return list.get(0).getId();
|
|
|
|
|
- } else {
|
|
|
|
|
- return null;
|
|
|
|
|
|
|
+
|
|
|
|
|
+ for (Container container : list) {
|
|
|
|
|
+ if (Arrays.stream(container.getNames()).collect(Collectors.toSet()).contains(containerName)) {
|
|
|
|
|
+ return container.getId();
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ return null;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
@@ -205,11 +207,8 @@ public class DockerImpl implements Docker {
|
|
|
|
|
|
|
|
public static void main(String[] args) throws DockerException {
|
|
public static void main(String[] args) throws DockerException {
|
|
|
Docker docker = new DockerImpl();
|
|
Docker docker = new DockerImpl();
|
|
|
- String appId = "dnkt-dev";
|
|
|
|
|
|
|
+ String appId = "dnkt";
|
|
|
String containerId = docker.getContainerIdByName(appId);
|
|
String containerId = docker.getContainerIdByName(appId);
|
|
|
- if (containerId != null) {
|
|
|
|
|
- InspectContainerResponse containerInfo = docker.inspectContainer(containerId);
|
|
|
|
|
- System.out.println();
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ System.out.println();
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|