diff --git a/core/src/main/java/org/testcontainers/DockerClientFactory.java b/core/src/main/java/org/testcontainers/DockerClientFactory.java index 0e68cc87965..693571802ad 100644 --- a/core/src/main/java/org/testcontainers/DockerClientFactory.java +++ b/core/src/main/java/org/testcontainers/DockerClientFactory.java @@ -97,13 +97,6 @@ public DockerClient client() { " Total Memory: " + dockerInfo.getMemTotal() / (1024 * 1024) + " MB"); checkVersion(version.getVersion()); - - List images = client.listImagesCmd().exec(); - // Pull the image we use to perform some checks - if (images.stream().noneMatch(it -> it.getRepoTags() != null && asList(it.getRepoTags()).contains(TINY_IMAGE))) { - client.pullImageCmd(TINY_IMAGE).exec(new PullImageResultCallback()).awaitSuccess(); - } - checkDiskSpaceAndHandleExceptions(client); preconditionsChecked = true; } @@ -111,6 +104,16 @@ public DockerClient client() { return client; } + /** + * Check whether the image is available locally and pull it otherwise + */ + private void checkAndPullImage(DockerClient client, String image) { + List images = client.listImagesCmd().withImageNameFilter(image).exec(); + if (images.isEmpty()) { + client.pullImageCmd(image).exec(new PullImageResultCallback()).awaitSuccess(); + } + } + /** * @return the IP address of the host running Docker */ @@ -168,6 +171,7 @@ public T runInsideDocker(Consumer createContainerCmdCons } private T runInsideDocker(DockerClient client, Consumer createContainerCmdConsumer, BiFunction block) { + checkAndPullImage(client, TINY_IMAGE); CreateContainerCmd createContainerCmd = client.createContainerCmd(TINY_IMAGE); createContainerCmdConsumer.accept(createContainerCmd); String id = createContainerCmd.exec().getId(); diff --git a/core/src/test/java/org/testcontainers/DockerClientFactoryTest.java b/core/src/test/java/org/testcontainers/DockerClientFactoryTest.java new file mode 100644 index 00000000000..bf67cc6885a --- /dev/null +++ b/core/src/test/java/org/testcontainers/DockerClientFactoryTest.java @@ -0,0 +1,30 @@ +package org.testcontainers; + +import org.junit.Test; +import org.testcontainers.dockerclient.LogToStringContainerCallback; +import org.testcontainers.utility.TestcontainersConfiguration; + +/** + * Test for {@link DockerClientFactory}. + */ +public class DockerClientFactoryTest { + + @Test + public void runCommandInsideDockerShouldNotFailIfImageDoesNotExistsLocally() { + + final DockerClientFactory dockFactory = DockerClientFactory.instance(); + //remove tiny image, so it will be pulled during next command run + dockFactory.client() + .removeImageCmd(TestcontainersConfiguration.getInstance().getTinyImage()) + .withForce(true).exec(); + + dockFactory.runInsideDocker( + cmd -> cmd.withCmd("sh", "-c", "echo 'SUCCESS'"), + (client, id) -> + client.logContainerCmd(id) + .withStdOut(true) + .exec(new LogToStringContainerCallback()) + .toString() + ); + } +} \ No newline at end of file