Search code examples
mongodbspring-bootgithub-actionstestcontainers

MongoDB testcontainer fails in github actions


I am running unit tests using MongoDB testconatiner defined as a spring bean using the following code

private static final MongoDBContainer mongoDBContainer = new MongoDBContainer(DockerImageName.parse(MONGO_IMAGE))
        .withReuse(true);

public MongoContainer() {
    mongoDBContainer.start();
    System.setProperty(MONGO_URI, mongoDBContainer.getReplicaSetUrl());
}

My github actions only setups java and runs a maven package command as following:

name: maven package
on:
 push:
   branches:
     - feature/github-actions
jobs:
  Explore-GitHub-Actions:
  runs-on: ubuntu-latest
  steps:
    - name: Check out repository code
      uses: actions/checkout@v3
    - name: Install Java
      uses: actions/setup-java@v3
      with:
        distribution: 'temurin'
        java-version: '17'
    - run: mvn clean package

I am always getting this error:

java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongoContainer' defined in file [/home/runner/work/kittelberger/kittelberger/target/test-classes/com/bosch/tt/kittelberger/utils/MongoContainer.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.bosch.tt.kittelberger.utils.MongoContainer]: Constructor threw exception; nested exception is org.testcontainers.containers.ContainerLaunchException: Container startup failed
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.bosch.tt.kittelberger.utils.MongoContainer]: Constructor threw exception; nested exception is org.testcontainers.containers.ContainerLaunchException: Container startup failed
Caused by: org.testcontainers.containers.ContainerLaunchException: Container startup failed
Caused by: org.rnorth.ducttape.RetryCountExceededException: Retry limit hit with exception
Caused by: org.testcontainers.containers.ContainerLaunchException: Could not create/start container
Caused by: org.testcontainers.containers.MongoDBContainer$ReplicaSetInitializationException: 
An error occurred: OCI runtime exec failed: exec failed: container_linux.go:380: starting container process caused: exec: "mongo": executable file not found in $PATH: unknown

Locally everything is working fine, there are other unit tests using other GenericContainers and they work fine which is strange. Thanks


Solution

  • The reason was that I used the latest mongo image where "mongo" was removed as Eddu pointed out. v5.0.12 fixes the issue.