HowTo run TestContainers MongoDBContainer with --auth

Did anyone manage to do it? I can’t make this mongo start with --auth mode and
MONGO_INITDB_DATABASE, MONGO_INITDB_ROOT_USERNAME,MONGO_INITDB_ROOT_PASSWORD

Hi @Ivan_Petrov, welcome to the community.
Can you please provide a screenshot of the error message that you are getting while performing the above?

In case you have any doubts, please feel free to reach out to us.

Thanks and Regards.
Sourabh Bagrecha,
Curriculum Services Engineer

val mongoContainer =
new MongoDBContainer(DockerImageName.parse(“mongo:4.0.10”)
.asCompatibleSubstituteFor(“mongo”))
.withCommand("–auth", “–replSet”, “docker-rs”)
.withEnv(“MONGO_INITDB_DATABASE”, “admin”)
.withEnv(“MONGO_INITDB_ROOT_USERNAME”, “admin”)
.withEnv(“MONGO_INITDB_ROOT_PASSWORD”, “whatever”)

mongoContainer.start()
2021-06-15 11:51:08,270 [ForkJoinPool-1-worker-29] INFO org.testcontainers.DockerClientFactory - Ryuk started - will monitor and terminate Testcontainers containers on JVM exit
2021-06-15 11:51:08,270 [ForkJoinPool-1-worker-29] INFO org.testcontainers.DockerClientFactory - Checking the system…
2021-06-15 11:51:08,270 [ForkJoinPool-1-worker-29] INFO org.testcontainers.DockerClientFactory - :heavy_check_mark:︎ Docker server version should be at least 1.6.0
2021-06-15 11:51:08,343 [ForkJoinPool-1-worker-29] INFO org.testcontainers.DockerClientFactory - :heavy_check_mark:︎ Docker environment should have more than 2GB free disk space
2021-06-15 11:51:46,218 [ForkJoinPool-1-worker-29] ERROR org.testcontainers.containers.MongoDBContainer - An error occurred: MongoDB shell version v4.0.10
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
2021-06-15T09:51:46.219+0000 E QUERY [js] Error: network error while attempting to run command ‘isMaster’ on host ‘127.0.0.1:27017’ :
connect@src/mongo/shell/mongo.js:344:17
@(connect):2:6

2021-06-15 11:51:46,219 [ForkJoinPool-1-worker-29] ERROR :whale: [mongo:4.0.10] - Could not start container
org.testcontainers.containers.MongoDBContainer$ReplicaSetInitializationException: An error occurred: MongoDB shell version v4.0.10
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
2021-06-15T09:51:46.219+0000 E QUERY [js] Error: network error while attempting to run command ‘isMaster’ on host ‘127.0.0.1:27017’ :
connect@src/mongo/shell/mongo.js:344:17
@(connect):2:6

@Ivan_Petrov it looks like you are trying to run a Docker container for this course - is that correct?

1 Like