in plugins/docker/base-image/src/main/java/co/elastic/gradle/dockerbase/DockerLocalImportArchiveTask.java [70:111]
public void localImport() throws IOException {
DockerUtils dockerUtils = new DockerUtils(getExecOperations());
final String imageId = getImageId().get();
if (imageExistsInDaemon(dockerUtils, imageId)) {
getLogger().lifecycle("Docker Daemon already has image with Id {}. Skip import.", imageId);
// The image might exist, but we want to make sure it's still tagged as we want it to
tagImage(dockerUtils, imageId);
} else {
final ByteArrayOutputStream out = new ByteArrayOutputStream();
try (InputStream archiveInput = ExtractCompressedTar.uncompressedInputStream(RegularFileUtils.toPath(getImageArchive()))) {
dockerUtils.exec(spec -> {
spec.setStandardInput(archiveInput);
spec.commandLine("docker", "load");
spec.setStandardOutput(out);
});
final String dockerLoad = out.toString().trim();
final String uuid;
if (dockerLoad.startsWith("Loaded image:") && dockerLoad.endsWith(":latest")) {
uuid = dockerLoad.substring(dockerLoad.indexOf(":") + 1);
} else {
throw new GradleException("Unexpected docker load output:" + dockerLoad);
}
// Tag image with the expected ID
tagImage(dockerUtils, imageId);
// Untag image with the internal UUID
dockerUtils.exec(spec ->
spec.commandLine("docker", "image", "rm", uuid.trim())
);
} catch (IOException e) {
throw new GradleException("Error importing image in docker daemon", e);
}
}
getLogger().lifecycle(
"Image tagged as {}",
getTag().get()
);
Files.writeString(
RegularFileUtils.toPath(getMarker()),
getTag().get()
);
}