diff --git a/core/src/main/java/org/testcontainers/DockerClientFactory.java b/core/src/main/java/org/testcontainers/DockerClientFactory.java index 0e68cc87965..c33c5d03b20 100644 --- a/core/src/main/java/org/testcontainers/DockerClientFactory.java +++ b/core/src/main/java/org/testcontainers/DockerClientFactory.java @@ -98,11 +98,8 @@ public DockerClient client() { 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(); - } + checkAndPullImage(client, TINY_IMAGE); checkDiskSpaceAndHandleExceptions(client); preconditionsChecked = true; @@ -111,6 +108,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 +175,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..5f5dbe1ac3a --- /dev/null +++ b/core/src/test/java/org/testcontainers/DockerClientFactoryTest.java @@ -0,0 +1,46 @@ +package org.testcontainers; + +import org.junit.Test; +import org.testcontainers.dockerclient.LogToStringContainerCallback; +import org.testcontainers.utility.TestcontainersConfiguration; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +/** + * Created mgorelikov on 05/04/2017 + * Test for {@link DockerClientFactory}. + */ +public class DockerClientFactoryTest { + + private static final String SUCCESS = "SUCCESS"; + + @Test + public void runCommandInsideDockerShouldPullImageIfItDoesNotExistsLocally() { + + final DockerClientFactory dockFactory = DockerClientFactory.instance(); + final String imageName = TestcontainersConfiguration.getInstance().getTinyImage(); + //remove tiny image, so it will be pulled during next command run + dockFactory.client() + .removeImageCmd(imageName) + .withForce(true).exec(); + + String result = dockFactory.runInsideDocker( + cmd -> cmd.withCmd("sh", "-c", "echo '" + SUCCESS + "'"), + (client, id) -> + client.logContainerCmd(id) + .withStdOut(true) + .exec(new LogToStringContainerCallback()) + .toString() + ); + //check local image availability + assertTrue(isImageAvailable(dockFactory, imageName)); + assertEquals(SUCCESS + '\n', result); + } + + private boolean isImageAvailable(DockerClientFactory dockFactory, String imageName) { + return !dockFactory.client().listImagesCmd() + .withImageNameFilter(imageName) + .exec().isEmpty(); + } +} \ No newline at end of file