为什么在使用测试容器时在测试结束时删除 docker 图像?

Why docker image is removed at the end of the tests when using testcontainers?

我正在使用测试容器:

protected static JdbcDatabaseContainer<?> platformPostgresContainer = new PostgreSQLContainer<>("postgres:13")
//      .withReuse(true)
      .withInitScript("create-databases.sql");

为什么没有 .withReuse(true) postgres:13 图像有时会在测试结束时从 Docker 中消失?

镜像被Ryuk删除,如果环境变量TESTCONTAINERS_RYUK_DISABLED=true没有指定,那么Testcontainers会启动Moby Ryuk container which in turn when will receive SIGTERM will remove all containers with label org.testcontainers=true, then will remove all images without containers (see the argument of cli.ImagesPrune from function prune in moby-ryuk/main.go)。因此,如果只有 postgres:13 个由 Testcontainers 创建的容器,那么在它们被删除后,postgres:13 图像将没有关联的容器并且也将被 Ryuk 删除。

这是设计使然,Testcontainers 管理的容器的生命周期可以挂接到测试框架中,但最终不会延长 JVM 进程的生命周期(除非使用 alpha 特性 resusable 模式) .