|
|
@@ -14,7 +14,9 @@ import com.github.dockerjava.httpclient5.ApacheDockerHttpClient;
|
|
|
import com.github.dockerjava.transport.DockerHttpClient;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
|
+import java.io.Closeable;
|
|
|
import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
import java.time.Duration;
|
|
|
import java.util.*;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
@@ -219,6 +221,94 @@ public class DockerImpl implements Docker {
|
|
|
return dockerClient.inspectContainerCmd(containerId).exec();
|
|
|
}
|
|
|
|
|
|
+ public void runAndRm(cn.reghao.devops.common.docker.model.Config containerConfig) throws Exception {
|
|
|
+ String image = containerConfig.getImage();
|
|
|
+ CreateContainerCmd createContainerCmd = dockerClient.createContainerCmd(image).withCmd("rm");
|
|
|
+
|
|
|
+ List<String> env = containerConfig.getEnv();
|
|
|
+ if (env != null) {
|
|
|
+ createContainerCmd.withEnv(env);
|
|
|
+ }
|
|
|
+
|
|
|
+ HostConfig hostConfig = HostConfig.newHostConfig()
|
|
|
+ .withNetworkMode("host")
|
|
|
+ .withRestartPolicy(RestartPolicy.noRestart());
|
|
|
+ if (containerConfig.getVolumes() != null) {
|
|
|
+ Map<String, String> map = containerConfig.getVolumes().getMap();
|
|
|
+ List<Bind> list = new ArrayList<>();
|
|
|
+ for (Map.Entry<String, String> entry : map.entrySet()) {
|
|
|
+ String key = entry.getKey();
|
|
|
+ String value = entry.getValue();
|
|
|
+ Volume volume2 = new Volume(value);
|
|
|
+ Bind bind = new Bind(key, volume2);
|
|
|
+ list.add(bind);
|
|
|
+ }
|
|
|
+ hostConfig.withBinds(list);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (containerConfig.getCmd() != null) {
|
|
|
+ createContainerCmd.withCmd(containerConfig.getCmd());
|
|
|
+ }
|
|
|
+
|
|
|
+ createContainerCmd.withHostConfig(hostConfig);
|
|
|
+ CreateContainerResponse response = createContainerCmd.exec();
|
|
|
+ String containerId = response.getId();
|
|
|
+ dockerClient.startContainerCmd(containerId).exec();
|
|
|
+
|
|
|
+ List<String> list = new ArrayList<>();
|
|
|
+ LogContainerCmd logContainerCmd = dockerClient.logContainerCmd(containerId);
|
|
|
+ logContainerCmd.withStdOut(true).withStdErr(true).withFollowStream(true);
|
|
|
+ logContainerCmd.exec(new ResultCallback<Frame>() {
|
|
|
+ @Override
|
|
|
+ public void onStart(Closeable closeable) {
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onNext(Frame object) {
|
|
|
+ if (object.getStreamType().equals(StreamType.STDERR)) {
|
|
|
+ if (list.size() > 100) {
|
|
|
+ list.clear();
|
|
|
+ }
|
|
|
+ list.add(object.toString());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onError(Throwable throwable) {
|
|
|
+ log.info(throwable.toString());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onComplete() {
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void close() throws IOException {
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ dockerClient.waitContainerCmd(containerId).exec(new ResultCallback.Adapter<>())
|
|
|
+ .awaitCompletion(300, TimeUnit.SECONDS);
|
|
|
+
|
|
|
+ InspectContainerResponse response1 = dockerClient.inspectContainerCmd(containerId).exec();
|
|
|
+ InspectContainerResponse.ContainerState state = response1.getState();
|
|
|
+ Long exitCode = state.getExitCodeLong();
|
|
|
+ if (Boolean.TRUE.equals(state.getRunning())) {
|
|
|
+ dockerClient.stopContainerCmd(containerId).exec();
|
|
|
+ dockerClient.removeContainerCmd(containerId).exec();
|
|
|
+ throw new Exception("docker build timeout");
|
|
|
+ } else if (exitCode != null && exitCode == 0) {
|
|
|
+ dockerClient.removeContainerCmd(containerId).exec();
|
|
|
+ } else {
|
|
|
+ dockerClient.removeContainerCmd(containerId).exec();
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ list.forEach(line -> {
|
|
|
+ sb.append(line).append(System.lineSeparator());
|
|
|
+ });
|
|
|
+ throw new Exception("docker build failed:\n" + sb.toString());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public InspectContainerResponse start(String containerName) {
|
|
|
String containerId = getContainerIdByName(containerName);
|