[Bug]: withReuse(true) does not reuse previously spun container #8814

@skrser

Description

Module

LocalStack

Testcontainers version

1.19.8

Using the latest Testcontainers version?

Host OS

Mac OS

Host Arch

Docker version

Client: Docker Engine - Community
 Version:           26.1.4
 API version:       1.43 (downgraded from 1.45)
 Go version:        go1.22.4
 Git commit:        5650f9b102
 Built:             Wed Jun  5 10:47:13 2024
 OS/Arch:           darwin/arm64
 Context:           default
Server: Docker Engine - Community
 Engine:
  Version:          24.0.9
  API version:      1.43 (minimum version 1.12)
  Go version:       go1.20.13
  Git commit:       fca702d
  Built:            Thu Feb  1 00:48:55 2024
  OS/Arch:          linux/arm64
  Experimental:     false
 containerd:
  Version:          1.6.28
  GitCommit:        ae07eda36dd25f8a1b98dfbf587313b99c0190bb
 runc:
  Version:          1.1.12
  GitCommit:        v1.1.12-0-g51d5e94
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

What happened?

LocalStackContainer(DockerImageName.parse("localstack/localstack:3.5.0")).withReuse(true)
does not reuse previously spun container.

Context

Recently, an MR was merged, which "Automatically add LAMBDA_DOCKER_FLAGS with testcontainer labels". Here is an example of what is added:

"LAMBDA_DOCKER_FLAGS=-l org.testcontainers=true -l org.testcontainers.lang=java -l org.testcontainers.version=1.19.8 -l org.testcontainers.sessionId=5a9ece76-30d4-43b9-9511-db1055f36d6e",

Where org.testcontainers.sessionId value is random UUID. This unique sessionId is part of CreateContainerCmd .

org.testcontainers.containers.GenericContainer.tryStart() has the following logic :

CreateContainerCmd createCommand = dockerClient.createContainerCmd(dockerImageName);
if (this.shouldBeReused) {
    String hash = hash(createCommand);
    containerId = findContainerForReuse(hash).orElse(null);

Since sessionId is always different, the hash is also different, which results in not reusing the existing container, and spinning up a new one each time.

If downgrade to 1.19.7, the container is reused.

Proposal

sessionId is ResourceReaper label. Containers run with withReuse(true) should be not reaped, thus there is no need to add this label.

Relevant log output

No response

Additional Information

No response