|
|
@@ -1,6 +1,8 @@
|
|
|
package cn.reghao.bnt.common.docker;
|
|
|
|
|
|
import cn.reghao.bnt.common.docker.po.Config;
|
|
|
+import cn.reghao.bnt.common.docker.po.ContainerInfo;
|
|
|
+import cn.reghao.bnt.common.docker.po.ImageInfo;
|
|
|
import cn.reghao.jutil.jdk.exception.ExceptionUtil;
|
|
|
import cn.reghao.jutil.jdk.converter.DateTimeConverter;
|
|
|
import cn.reghao.jutil.jdk.text.TextFile;
|
|
|
@@ -16,7 +18,9 @@ import com.github.dockerjava.transport.DockerHttpClient;
|
|
|
|
|
|
import java.io.File;
|
|
|
import java.time.Duration;
|
|
|
+import java.time.LocalDateTime;
|
|
|
import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* Docker 客户端
|
|
|
@@ -25,7 +29,7 @@ import java.util.*;
|
|
|
* @date 2021-10-27 03:41:38
|
|
|
*/
|
|
|
public class DockerImpl implements Docker {
|
|
|
- private final DockerClient dockerClient;
|
|
|
+ private DockerClient dockerClient;
|
|
|
private final TextFile textFile = new TextFile();
|
|
|
|
|
|
public DockerImpl() {
|
|
|
@@ -38,7 +42,23 @@ public class DockerImpl implements Docker {
|
|
|
//.withRegistryEmail(registryMail)
|
|
|
//.withRegistryUrl(registryUrl)
|
|
|
.build();
|
|
|
+ init(config);
|
|
|
+ }
|
|
|
+
|
|
|
+ public DockerImpl(String username, String password) {
|
|
|
+ DockerClientConfig config = DefaultDockerClientConfig.createDefaultConfigBuilder()
|
|
|
+ .withDockerHost("unix:///var/run/docker.sock")
|
|
|
+ .withDockerTlsVerify(false)
|
|
|
+ .withDockerCertPath(String.format("%s/.docker", System.getProperty("user.home")))
|
|
|
+ .withRegistryUsername("username")
|
|
|
+ .withRegistryPassword("password")
|
|
|
+ //.withRegistryEmail(registryMail)
|
|
|
+ //.withRegistryUrl(registryUrl)
|
|
|
+ .build();
|
|
|
+ init(config);
|
|
|
+ }
|
|
|
|
|
|
+ private void init(DockerClientConfig config) {
|
|
|
DockerHttpClient httpClient = new ApacheDockerHttpClient.Builder()
|
|
|
.dockerHost(config.getDockerHost())
|
|
|
.sslConfig(config.getSSLConfig())
|
|
|
@@ -46,7 +66,6 @@ public class DockerImpl implements Docker {
|
|
|
.connectionTimeout(Duration.ofSeconds(30))
|
|
|
.responseTimeout(Duration.ofSeconds(45))
|
|
|
.build();
|
|
|
-
|
|
|
this.dockerClient = DockerClientImpl.getInstance(config, httpClient);
|
|
|
}
|
|
|
|
|
|
@@ -236,30 +255,47 @@ public class DockerImpl implements Docker {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public List<Image> images() {
|
|
|
+ public List<ImageInfo> images() {
|
|
|
List<Image> images = dockerClient.listImagesCmd().exec();
|
|
|
-
|
|
|
- images.forEach(image -> {
|
|
|
+ return images.stream().map(image -> {
|
|
|
long created = image.getCreated();
|
|
|
String[] repoTags = image.getRepoTags();
|
|
|
if (repoTags.length == 0) {
|
|
|
- return;
|
|
|
+ return null;
|
|
|
}
|
|
|
|
|
|
+ String imageId = image.getId();
|
|
|
String[] repoTag = repoTags[0].split(":");
|
|
|
String repo = repoTag[0];
|
|
|
String tag = repoTag[1];
|
|
|
- System.out.printf("%s:%s -> %s\n", repo, tag, DateTimeConverter.format(created*1000));
|
|
|
- });
|
|
|
+ LocalDateTime created1 = DateTimeConverter.localDateTime(created*1000);
|
|
|
+ return new ImageInfo(repo, tag, imageId, created1);
|
|
|
+ }).filter(Objects::nonNull).collect(Collectors.toList());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void imageRm(String imageId) {
|
|
|
+ dockerClient.removeImageCmd(imageId).exec();
|
|
|
+ }
|
|
|
|
|
|
- return images;
|
|
|
+ @Override
|
|
|
+ public void rm(String containerId) {
|
|
|
+ dockerClient.removeContainerCmd(containerId).exec();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public List<Container> ps(boolean isAll) {
|
|
|
- return dockerClient.listContainersCmd()
|
|
|
- .withShowAll(isAll)
|
|
|
+ public List<ContainerInfo> psAll() {
|
|
|
+ List<Container> list = dockerClient.listContainersCmd()
|
|
|
+ .withShowAll(true)
|
|
|
.exec();
|
|
|
+
|
|
|
+ return list.stream().map(container -> {
|
|
|
+ String containerId = container.getId();
|
|
|
+ String state = container.getState();
|
|
|
+ boolean running = "running".equalsIgnoreCase(state);
|
|
|
+ String imageId = container.getImageId();
|
|
|
+ return new ContainerInfo(containerId, running, imageId);
|
|
|
+ }).collect(Collectors.toList());
|
|
|
}
|
|
|
|
|
|
@Override
|