For the example, I'm using SpringBoot in DevMode. It is the only mode I consider that makes sense to enable the volume mapping.
Remember to add .data/ to .gitignore
| plugins { | |
| java | |
| idea | |
| id("org.springframework.boot") version "3.1.3" | |
| id("io.spring.dependency-management") version "1.1.0" | |
| } | |
| group = "dev.aleixmorgadas" | |
| version = "0.0.1" | |
| java.sourceCompatibility = JavaVersion.VERSION_17 | |
| configurations { | |
| compileOnly { | |
| extendsFrom(configurations.annotationProcessor.get()) | |
| } | |
| } | |
| repositories { | |
| mavenCentral() | |
| } | |
| extra["testcontainersVersion"] = "1.18.3" | |
| dependencies { | |
| // Web | |
| implementation("org.springframework.boot:spring-boot-starter-web") | |
| // Persistence | |
| implementation("org.springframework.boot:spring-boot-starter-data-mongodb") | |
| // Testing | |
| testImplementation("org.springframework.boot:spring-boot-starter-test") | |
| testImplementation("org.springframework.boot:spring-boot-devtools") | |
| testImplementation("org.springframework.boot:spring-boot-testcontainers") | |
| testImplementation("org.testcontainers:junit-jupiter") | |
| testImplementation("org.testcontainers:mongodb") | |
| testRuntimeOnly("org.junit.platform:junit-platform-launcher") | |
| } | |
| dependencyManagement { | |
| imports { | |
| mavenBom("org.testcontainers:testcontainers-bom:${property("testcontainersVersion")}") | |
| } | |
| } | |
| tasks.withType<Test> { | |
| useJUnitPlatform() | |
| } |
| package dev.aleixmorgadas; | |
| import org.springframework.boot.SpringApplication; | |
| import org.springframework.boot.devtools.restart.RestartScope; | |
| import org.springframework.boot.test.context.TestConfiguration; | |
| import org.springframework.boot.testcontainers.service.connection.ServiceConnection; | |
| import org.springframework.context.annotation.Bean; | |
| import org.testcontainers.containers.BindMode; | |
| import org.testcontainers.containers.MongoDBContainer; | |
| import org.testcontainers.utility.DockerImageName; | |
| import java.util.List; | |
| @TestConfiguration(proxyBeanMethods = false) | |
| public class RunApplication { | |
| public static void main(String[] args) { | |
| SpringApplication.from(EnablingFlowApplication::main) | |
| .with(RunApplication.class) | |
| .run(args); | |
| } | |
| @Bean | |
| @RestartScope | |
| @ServiceConnection | |
| MongoDBContainer mongoContainer() { | |
| return new MongoDBContainer(DockerImageName.parse("mongo:6.0.8")) { | |
| @Override | |
| public void configure() { | |
| withFileSystemBind("./.data", "/data/db"); | |
| waitingFor(Wait.forLogMessage("(?i).*waiting for connections.*", 1)); | |
| setPortBindings(List.of("55555:27017")); | |
| } | |
| @Override | |
| protected void containerIsStarted(InspectContainerResponse containerInfo, boolean reused) { | |
| // Disable default configuration | |
| } | |
| }; | |
| } | |
| } |