|
1 | 1 | package com.formkiq.gradle.services; |
2 | 2 |
|
3 | | -import static com.formkiq.gradle.internal.NativeImageExecutor.GRAALVM_JAVA_MAIN; |
4 | | - |
5 | 3 | import com.github.dockerjava.api.DockerClient; |
6 | | -import com.github.dockerjava.api.command.BuildImageResultCallback; |
7 | 4 | import com.github.dockerjava.api.command.CreateContainerResponse; |
8 | 5 | import com.github.dockerjava.api.exception.NotFoundException; |
9 | 6 | import com.github.dockerjava.api.model.Bind; |
|
14 | 11 | import com.github.dockerjava.core.DockerClientConfig; |
15 | 12 | import com.github.dockerjava.httpclient5.ApacheDockerHttpClient; |
16 | 13 | import com.github.dockerjava.transport.DockerHttpClient; |
17 | | -import java.io.File; |
18 | 14 | import java.io.IOException; |
19 | 15 | import java.nio.file.Files; |
20 | 16 | import java.nio.file.Path; |
21 | 17 | import java.time.Duration; |
22 | 18 | import java.util.Collections; |
| 19 | +import org.gradle.api.logging.Logger; |
23 | 20 |
|
24 | 21 | /** Default implementation using docker-java client (socket first, then TCP). */ |
25 | 22 | public final class DefaultDockerService implements DockerService { |
26 | 23 | private final DockerClient dockerClient; |
27 | 24 |
|
28 | | - /** constructor. */ |
29 | | - public DefaultDockerService() { |
| 25 | + /** {@link Logger}. */ |
| 26 | + private final Logger log; |
| 27 | + |
| 28 | + /** {@link LoggingBuildImageResultCallback}. */ |
| 29 | + private final LoggingBuildImageResultCallback callback; |
| 30 | + |
| 31 | + /** |
| 32 | + * constructor. |
| 33 | + * |
| 34 | + * @param logger {@link Logger} |
| 35 | + */ |
| 36 | + public DefaultDockerService(final Logger logger) { |
30 | 37 | this.dockerClient = initClient(); |
| 38 | + this.log = logger; |
| 39 | + this.callback = new LoggingBuildImageResultCallback(this.log); |
31 | 40 | } |
32 | 41 |
|
33 | 42 | private DockerClient initClient() { |
@@ -68,40 +77,61 @@ public boolean isDockerRunning() { |
68 | 77 |
|
69 | 78 | @Override |
70 | 79 | public Path buildDockerImage(final Path buildDir, final String imageTag, |
71 | | - final String dockerFileContent) throws IOException { |
| 80 | + final String dockerFileContent, final Path contextDir) throws IOException { |
72 | 81 |
|
73 | 82 | Path dockerfile = writeDockerFile(buildDir, dockerFileContent); |
74 | 83 |
|
75 | | - File contextDir = buildDir.resolve(GRAALVM_JAVA_MAIN).toFile(); |
| 84 | + String dockerCommand = |
| 85 | + String.format("docker build -f %s -t %s %s", dockerfile, imageTag, contextDir); |
76 | 86 |
|
77 | | - dockerClient.buildImageCmd().withBaseDirectory(contextDir).withDockerfile(dockerfile.toFile()) |
78 | | - .withTags(Collections.singleton(imageTag)).exec(new BuildImageResultCallback()) |
79 | | - .awaitImageId(); |
| 87 | + log(dockerCommand); |
| 88 | + dockerClient.buildImageCmd().withBaseDirectory(contextDir.toFile()) |
| 89 | + .withDockerfile(dockerfile.toFile()).withTags(Collections.singleton(imageTag)) |
| 90 | + .exec(this.callback).awaitImageId(); |
80 | 91 |
|
81 | 92 | return dockerfile; |
82 | 93 | } |
83 | 94 |
|
| 95 | + private void log(final String log) { |
| 96 | + if (this.log != null) { |
| 97 | + this.log.info(log); |
| 98 | + } |
| 99 | + } |
| 100 | + |
84 | 101 | @Override |
85 | 102 | public void runDockerImage(final Path buildDir, final String imageTag) |
86 | 103 | throws IOException, InterruptedException { |
87 | 104 |
|
88 | 105 | Path path = buildDir.resolve("output"); |
89 | 106 | Files.createDirectories(path); |
90 | 107 |
|
| 108 | + String containerName = "copy-file-container-" + System.currentTimeMillis(); |
| 109 | + String hostPath = path.toAbsolutePath().toString(); |
| 110 | + |
| 111 | + log(String.format("docker run --name %s -v %s:/output %s", containerName, hostPath, imageTag)); |
| 112 | + |
91 | 113 | Volume containerOutputVolume = new Volume("/output"); |
92 | | - HostConfig hostConfig = HostConfig.newHostConfig() |
93 | | - .withBinds(new Bind(path.toAbsolutePath().toString(), containerOutputVolume)); |
| 114 | + HostConfig hostConfig = |
| 115 | + HostConfig.newHostConfig().withBinds(new Bind(hostPath, containerOutputVolume)); |
94 | 116 |
|
| 117 | + log("Creating container '" + containerName + "' from image '" + imageTag + "'"); |
95 | 118 | CreateContainerResponse container = dockerClient.createContainerCmd(imageTag) |
96 | | - .withName("copy-file-container-" + System.currentTimeMillis()).withHostConfig(hostConfig) |
97 | | - .exec(); |
| 119 | + .withName(containerName).withHostConfig(hostConfig).exec(); |
98 | 120 |
|
99 | 121 | String containerId = container.getId(); |
100 | | - dockerClient.startContainerCmd(containerId).exec(); |
| 122 | + log("Container created with ID: " + containerId); |
| 123 | + |
101 | 124 | try { |
| 125 | + log("Starting container " + containerId); |
| 126 | + dockerClient.startContainerCmd(containerId).exec(); |
| 127 | + |
| 128 | + log("Waiting for container " + containerId + " to finish"); |
102 | 129 | dockerClient.waitContainerCmd(containerId).start().awaitCompletion(); |
| 130 | + log("Container " + containerId + " finished successfully"); |
103 | 131 | } finally { |
| 132 | + log("Removing container " + containerId + " (force=true)"); |
104 | 133 | dockerClient.removeContainerCmd(containerId).withForce(true).exec(); |
| 134 | + log("Container " + containerId + " removed"); |
105 | 135 | } |
106 | 136 | } |
107 | 137 |
|
|
0 commit comments