diff --git a/core/src/main/java/org/testcontainers/images/ParsedDockerfile.java b/core/src/main/java/org/testcontainers/images/ParsedDockerfile.java index ed88c1a85ff..8c4d7866ff4 100644 --- a/core/src/main/java/org/testcontainers/images/ParsedDockerfile.java +++ b/core/src/main/java/org/testcontainers/images/ParsedDockerfile.java @@ -29,17 +29,17 @@ public class ParsedDockerfile { private final Path dockerFilePath; @Getter - private Set dependencyImageNames = Collections.emptySet(); + private final Set dependencyImageNames; public ParsedDockerfile(Path dockerFilePath) { this.dockerFilePath = dockerFilePath; - parse(read()); + this.dependencyImageNames = parse(read()); } @VisibleForTesting ParsedDockerfile(List lines) { this.dockerFilePath = Paths.get("dummy.Dockerfile"); - parse(lines); + this.dependencyImageNames = parse(lines); } private List read() { @@ -56,17 +56,17 @@ private List read() { } } - private void parse(List lines) { - dependencyImageNames = - lines - .stream() - .map(FROM_LINE_PATTERN::matcher) - .filter(Matcher::matches) - .map(matcher -> matcher.group("image")) - .collect(Collectors.toSet()); + private Set parse(List lines) { + Set imageNames = lines + .stream() + .map(FROM_LINE_PATTERN::matcher) + .filter(Matcher::matches) + .map(matcher -> matcher.group("image")) + .collect(Collectors.toSet()); - if (!dependencyImageNames.isEmpty()) { - log.debug("Found dependency images in Dockerfile {}: {}", dockerFilePath, dependencyImageNames); + if (!imageNames.isEmpty()) { + log.debug("Found dependency images in Dockerfile {}: {}", dockerFilePath, imageNames); } + return imageNames; } } diff --git a/core/src/main/java/org/testcontainers/images/builder/ImageFromDockerfile.java b/core/src/main/java/org/testcontainers/images/builder/ImageFromDockerfile.java index 201cf0fd058..c453c24a779 100644 --- a/core/src/main/java/org/testcontainers/images/builder/ImageFromDockerfile.java +++ b/core/src/main/java/org/testcontainers/images/builder/ImageFromDockerfile.java @@ -50,7 +50,7 @@ public class ImageFromDockerfile private final String dockerImageName; - private boolean deleteOnExit = true; + private final boolean deleteOnExit; private final Map transferables = new HashMap<>(); @@ -90,6 +90,7 @@ public ImageFromDockerfile withFileFromTransferable(String path, Transferable tr protected final String resolve() { Logger logger = DockerLoggerFactory.getLogger(dockerImageName); + //noinspection resource DockerClient dockerClient = DockerClientFactory.instance().client(); try { @@ -101,12 +102,12 @@ public void onNext(BuildResponseItem item) { if (item.isErrorIndicated()) { logger.error(item.getErrorDetail().getMessage()); } else { - logger.debug(StringUtils.chomp(item.getStream(), "\n")); + logger.debug(StringUtils.removeEnd(item.getStream(), "\n")); } } }; - // We have to use pipes to avoid high memory consumption since users might want to build really big images + // We have to use pipes to avoid high memory consumption since users might want to build huge images @Cleanup PipedInputStream in = new PipedInputStream(); @Cleanup @@ -163,7 +164,7 @@ public void onNext(BuildResponseItem item) { } protected void configure(BuildImageCmd buildImageCmd) { - buildImageCmd.withTag(this.getDockerImageName()); + buildImageCmd.withTags(Collections.singleton(getDockerImageName())); this.dockerFilePath.ifPresent(buildImageCmd::withDockerfilePath); this.dockerfile.ifPresent(p -> { buildImageCmd.withDockerfile(p.toFile()); @@ -180,8 +181,6 @@ protected void configure(BuildImageCmd buildImageCmd) { } private void prePullDependencyImages(Set imagesToPull) { - final DockerClient dockerClient = DockerClientFactory.instance().client(); - imagesToPull.forEach(imageName -> { try { log.info( diff --git a/core/src/test/java/org/testcontainers/images/builder/ImageFromDockerfileTest.java b/core/src/test/java/org/testcontainers/images/builder/ImageFromDockerfileTest.java index abc05eb567c..c9b54d58241 100644 --- a/core/src/test/java/org/testcontainers/images/builder/ImageFromDockerfileTest.java +++ b/core/src/test/java/org/testcontainers/images/builder/ImageFromDockerfileTest.java @@ -16,10 +16,12 @@ public void shouldAddDefaultLabels() { String imageId = image.resolve(); + //noinspection resource DockerClient dockerClient = DockerClientFactory.instance().client(); InspectImageResponse inspectImageResponse = dockerClient.inspectImageCmd(imageId).exec(); + assertThat(inspectImageResponse.getConfig()).isNotNull(); assertThat(inspectImageResponse.getConfig().getLabels()) .containsAllEntriesOf(DockerClientFactory.DEFAULT_LABELS); } @@ -32,10 +34,13 @@ public void shouldNotAddSessionLabelIfDeleteOnExitIsFalse() { ) .withDockerfileFromBuilder(it -> it.from("scratch")); String imageId = image.resolve(); + + //noinspection resource DockerClient dockerClient = DockerClientFactory.instance().client(); try { InspectImageResponse inspectImageResponse = dockerClient.inspectImageCmd(imageId).exec(); + assertThat(inspectImageResponse.getConfig()).isNotNull(); assertThat(inspectImageResponse.getConfig().getLabels()) .doesNotContainKey(DockerClientFactory.TESTCONTAINERS_SESSION_ID_LABEL); } finally {