testcontainers / testcontainers/testcontainers-java

ImageFromDockerfile(dockerImageName, deleteOnExit) failing to build container doesn't error usefully

Open
#3,713 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
8.7k
Forks
1.9k
Avg merge
2d 17h
Merged PRs (30d)
9

Description

I spent a couple of hours this morning chasing down an issue where a developer was able to use ImageFromDockerfile to build an image locally but it was failing in CI and on other machines despite running docker system prune -a on their machine to clear any possible cached images.

The stacktrace showed it oddly attempting to pull the image from Docker Hub and it failing because it doesn't exist there:

Container startup failed
org.testcontainers.containers.ContainerLaunchException: Container startup failed
	at org.testcontainers.containers.GenericContainer.doStart(GenericContainer.java:330)
	at org.testcontainers.containers.GenericContainer.start(GenericContainer.java:311)
	at com.cloudnc.schedulesolver.services.integration.IntegrationTest.beforeSpec(IntegrationTest.kt:46)
	at io.kotlintest.runner.jvm.spec.SpecRunner.executeBeforeSpec(SpecRunner.kt:40)
	at io.kotlintest.runner.jvm.spec.SpecRunner.interceptSpec(SpecRunner.kt:26)
	at io.kotlintest.runner.jvm.spec.SpecRunner.interceptSpec(SpecRunner.kt:53)
	at io.kotlintest.runner.jvm.spec.SingleInstanceSpecRunner$execute$1.invokeSuspend(SingleInstanceSpecRunner.kt:53)
	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
	at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
	at kotlinx.coroutines.EventLoopImplBase.processNextEvent(EventLoop.common.kt:274)
	at kotlinx.coroutines.BlockingCoroutine.joinBlocking(Builders.kt:86)
	at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking(Builders.kt:61)
	at kotlinx.coroutines.BuildersKt.runBlocking(Unknown Source)
	at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking$default(Builders.kt)
	at kotlinx.coroutines.BuildersKt.runBlocking$default(Unknown Source)
	at io.kotlintest.runner.jvm.spec.SingleInstanceSpecRunner.execute(SingleInstanceSpecRunner.kt:52)
	at io.kotlintest.runner.jvm.spec.SpecExecutor$execute$$inlined$invoke$lambda$1.invoke(SpecExecutor.kt:54)
	at io.kotlintest.runner.jvm.spec.SpecExecutor$execute$$inlined$invoke$lambda$1.invoke(SpecExecutor.kt:20)
	at io.kotlintest.runner.jvm.spec.SpecExecutor.withExecutor(SpecExecutor.kt:31)
	at io.kotlintest.runner.jvm.spec.SpecExecutor.execute(SpecExecutor.kt:37)
	at io.kotlintest.runner.jvm.TestEngine$submitSpec$1.run(TestEngine.kt:111)
	at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
	at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
	at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: org.testcontainers.containers.ContainerFetchException: Can't get Docker image: RemoteDockerImage(imageName=test/schedule-service:latest, imagePullPolicy=DefaultPullPolicy())
	at org.testcontainers.containers.GenericContainer.getDockerImageName(GenericContainer.java:1286)
	at org.testcontainers.containers.GenericContainer.logger(GenericContainer.java:615)
	at org.testcontainers.containers.GenericContainer.doStart(GenericContainer.java:320)
	... 25 more
Caused by: com.github.dockerjava.api.exception.NotFoundException: Status 404: {"message":"pull access denied for test/schedule-service, repository does not exist or may require 'docker login': denied: requested access to the resource is denied"}
	at org.testcontainers.shaded.com.github.dockerjava.core.DefaultInvocationBuilder.execute(DefaultInvocationBuilder.java:241)
	at org.testcontainers.shaded.com.github.dockerjava.core.DefaultInvocationBuilder.lambda$executeAndStream$1(DefaultInvocationBuilder.java:269)
	... 1 more
com.cloudnc.schedulesolver.services.integration.IntegrationTest > initializationError FAILED
    org.testcontainers.containers.ContainerLaunchException at IntegrationTest.kt:46
        Caused by: org.testcontainers.containers.ContainerFetchException at IntegrationTest.kt:46
            Caused by: com.github.dockerjava.api.exception.NotFoundException at DefaultInvocationBuilder.java:241

When I removed the dockerImageName and deleteOnExit arguments from the method then it gave me a much more useful error which showed the problem:

Container startup failed
org.testcontainers.containers.ContainerLaunchException: Container startup failed
	at org.testcontainers.containers.GenericContainer.doStart(GenericContainer.java:330)
	at org.testcontainers.containers.GenericContainer.start(GenericContainer.java:311)
	at com.cloudnc.schedulesolver.services.integration.IntegrationTest.beforeSpec(IntegrationTest.kt:46)
	at io.kotlintest.runner.jvm.spec.SpecRunner.executeBeforeSpec(SpecRunner.kt:40)
	at io.kotlintest.runner.jvm.spec.SpecRunner.interceptSpec(SpecRunner.kt:26)
	at io.kotlintest.runner.jvm.spec.SpecRunner.interceptSpec(SpecRunner.kt:53)
	at io.kotlintest.runner.jvm.spec.SingleInstanceSpecRunner$execute$1.invokeSuspend(SingleInstanceSpecRunner.kt:53)
	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
	at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
	at kotlinx.coroutines.EventLoopImplBase.processNextEvent(EventLoop.common.kt:274)
	at kotlinx.coroutines.BlockingCoroutine.joinBlocking(Builders.kt:86)
	at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking(Builders.kt:61)
	at kotlinx.coroutines.BuildersKt.runBlocking(Unknown Source)
	at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking$default(Builders.kt)
	at kotlinx.coroutines.BuildersKt.runBlocking$default(Unknown Source)
	at io.kotlintest.runner.jvm.spec.SingleInstanceSpecRunner.execute(SingleInstanceSpecRunner.kt:52)
	at io.kotlintest.runner.jvm.spec.SpecExecutor$execute$$inlined$invoke$lambda$1.invoke(SpecExecutor.kt:54)
	at io.kotlintest.runner.jvm.spec.SpecExecutor$execute$$inlined$invoke$lambda$1.invoke(SpecExecutor.kt:20)
	at io.kotlintest.runner.jvm.spec.SpecExecutor.withExecutor(SpecExecutor.kt:31)
	at io.kotlintest.runner.jvm.spec.SpecExecutor.execute(SpecExecutor.kt:37)
	at io.kotlintest.runner.jvm.TestEngine$submitSpec$1.run(TestEngine.kt:111)
	at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
	at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
	at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: org.testcontainers.containers.ContainerFetchException: Can't get Docker image: RemoteDockerImage(imageName=<resolving>, imagePullPolicy=DefaultPullPolicy())
	at org.testcontainers.containers.GenericContainer.getDockerImageName(GenericContainer.java:1286)
	at org.testcontainers.containers.GenericContainer.logger(GenericContainer.java:615)
	at org.testcontainers.containers.GenericContainer.doStart(GenericContainer.java:320)
	... 25 more
Caused by: com.github.dockerjava.api.exception.DockerClientException: Could not build image: COPY failed: file not found in build context or excluded by .dockerignore: stat build/libs/schedule-solver-app.jar: file does not exist
	at com.github.dockerjava.api.command.BuildImageResultCallback.getImageId(BuildImageResultCallback.java:78)
	at com.github.dockerjava.api.command.BuildImageResultCallback.awaitImageId(BuildImageResultCallback.java:50)
	at org.testcontainers.images.builder.ImageFromDockerfile.resolve(ImageFromDockerfile.java:141)
	at org.testcontainers.images.builder.ImageFromDockerfile.resolve(ImageFromDockerfile.java:37)
	at org.testcontainers.utility.LazyFuture.getResolvedValue(LazyFuture.java:17)
	at org.testcontainers.utility.LazyFuture.get(LazyFuture.java:39)
	at org.testcontainers.shaded.com.google.common.util.concurrent.Futures$3.get(Futures.java:1332)
	at org.testcontainers.images.RemoteDockerImage.getImageName(RemoteDockerImage.java:104)
	at org.testcontainers.images.RemoteDockerImage.resolve(RemoteDockerImage.java:63)
	at org.testcontainers.images.RemoteDockerImage.resolve(RemoteDockerImage.java:27)
	at org.testcontainers.utility.LazyFuture.getResolvedValue(LazyFuture.java:17)
	at org.testcontainers.utility.LazyFuture.get(LazyFuture.java:39)
	at org.testcontainers.containers.GenericContainer.getDockerImageName(GenericContainer.java:1284)
	... 27 more

The Dockerfile is attempting to copy in a jar that should have been built in an earlier step but was missed. It worked on the developer's local machine because they had the jar already built.

I'm not sure why passing the dockerImageName and deleteOnExit arguments to the method causes it to fail quietly and then attempt to run the image and so fetch from Docker Hub while the argumentless version gives me the more useful error message here?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Begin with ImageFromDockerfile.resolve and the GenericContainer.getDockerImageName/RemoteDockerImage path shown in the trace; compare it with the argumentless path. Reproduce the missing build/libs/schedule-solver-app.jar case with dockerImageName and deleteOnExit, then ensure the underlying Docker build failure remains visible instead of triggering a pull of test/schedule-service:latest.

Written by the indexing model from the issue text.

Assessment

Tech stack
docker, java
Domain
testing
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.