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条)