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. 😀