mongodb - TestContainers Mongo fails to start, but Docker Compose with same config is OK - Stack Overflow

I have a Docker compose.yml for MongoDB, and this works.mongodb:image: mongo:8.0.4-noblecontainer_name

I have a Docker compose.yml for MongoDB, and this works.

mongodb:
    image: mongo:8.0.4-noble
    container_name: mongodb
    hostname: mongodb
    restart: unless-stopped
    volumes:
        - mongodb-data:/var/lib/mongodb/
        - mongodb-log:/var/log/mongodb/
        - ../src/main/database/mongo/mongod.conf:/etc/mongod.conf:ro
        - ../src/main/database/mongo/mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js:ro
    environment:
      MONGO_INITDB_ROOT_USERNAME: mongo
      MONGO_INITDB_ROOT_PASSWORD: mongo
      MONGO_INITDB_DATABASE: mongo
    ports:
      - '27017:27017'

I'm now trying to mirror this config in a Java unit test with TestContainers:

public MongoDBContainer mongoDBContainer() {
        return new MongoDBContainer("mongo:8.0.4-noble")
            .withEnv("MONGO_INITDB_ROOT_USERNAME", "mongo")
            .withEnv("MONGO_INITDB_ROOT_PASSWORD", "mongo" )
            .withEnv("MONGO_INITDB_DATABASE", "mongo" )
            .withExposedPorts(27017)
            // Maven copies these files onto the classpath
            .withCopyFileToContainer(
                forClasspathResource( "/mongo/mongod.conf"), "/etc/mongod.conf" )
            .withCopyFileToContainer(
                forClasspathResource( "/mongo/mongo-init.js"), "/docker-entrypoint-initdb.d/mongo-init.js" )
            .waitingFor( Wait.forListeningPort() )
            .withReuse( true );
    }

This fails with

.testcontainers.containers.MongoDBContainer$ReplicaSetInitializationException: An error occurred: 

This is mongod.conf in both cases:

storage:
  dbPath: /var/lib/mongodb

systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

processManagement:
  timeZoneInfo: /usr/share/zoneinfo

I have no idea how to correctly configure the replicaset which TestContainers seems to want. For dev and testing, I was figuring that a standalone database would be easiest.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744733155a4590583.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信