“ContainerLaunchException: Container startup failed” when running Testcontainers with Gradle

I had this nasty error when running integration tests with Testcontainers for my Java project with Gradle:

SEVERE: Caught exception while closing extension context: org.junit.jupiter.engine.descriptor.ClassExtensionContext@669c884
org.testcontainers.containers.ContainerLaunchException: Container startup failed
...
    Caused by: org.testcontainers.containers.ContainerFetchException: Can't get Docker image: RemoteDockerImage(imageName=, imagePullPolicy=DefaultPullPolicy())
    ...
        Caused by: com.github.dockerjava.api.exception.DockerClientException: Error occurred while preparing Docker context folder.
        ...
            Caused by: java.io.IOException: Der Prozess kann nicht auf die Datei zugreifen, da ein anderer Prozess einen Teil der Datei gesperrt hat

The last line says The process cannot access the file because it is being used by another process in German.

The integration test builds the application from a Dockerfile like this:

@Container
public static GenericContainer DG_CONTAINER = new GenericContainer<>(
    new ImageFromDockerfile()
        .withDockerfile(Paths.get("./Dockerfile.build")))
            .waitingFor(...);

The problem – as I interpret it after searching for a solution for quite some time – was Docker trying to build the application including all (sub-)folders of the current project, including working directories like .gradle, that were locked because Gradle ran the integration test.

The solution was quite simple: I added the file .dockerignore to my project and exluded all unneeded directories. In my case the file looks like this:

.gradle/
bin/
.settings/

Now the integration test runs smoothly with Gradle. 😀

Leave a Comment

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax