Skip to content

feat(compose): added ryuk cleanup functionality for compose projects - #1128

Open
TimNuga wants to merge 1 commit into
testcontainers:mainfrom
TimNuga:feat/ryuk-cleanup-for-compose-projects
Open

TimNuga wants to merge 1 commit into
testcontainers:mainfrom
TimNuga:feat/ryuk-cleanup-for-compose-projects

Conversation

@TimNuga

@TimNuga TimNuga commented Sep 29, 2026

Copy link
Copy Markdown

What does this PR do?

Adds opt-in Ryuk cleanup to DockerCompose:

with DockerCompose("./services", ryuk=True) as compose:
    # Run integration tests.
    ...

Each opted-in object receives a unique Compose project name and registers its com.docker.compose.project label with the process’s shared Reaper before starting services. If the Python process is abruptly terminated, Ryuk can remove the project’s containers, networks, and volumes.

Existing behavior remains unchanged when ryuk is omitted.

Why is it needed?

DockerCompose starts services through the Compose CLI, bypassing the Reaper registration used by DockerContainer. Context-manager cleanup handles normal exit, but an abruptly terminated test process can leave Compose resources behind.

This connects Compose environments to the existing cleanup mechanism. Registering the project before compose up also covers resources left behind when startup fails partway through. A unique project name separates cleanup ownership between independent Compose objects using the same files.

Addresses #1049.

Shape

Extends the existing DockerCompose dataclass and shared command builder. The generated project name is reused across all commands and restarts of the same object. Compose reuses an existing Reaper when available.

Adds acknowledged label-filter registration to Reaper, including serialized socket exchanges, fragmented-response handling, and a bounded timeout. Initial session registration uses the same mechanism so its acknowledgement cannot be mistaken for a subsequent Compose registration. Registration failures prevent startup; a failed additional registration does not deliberately close the shared connection and trigger cleanup of other environments.

Added documentation and 22 test cases covering registration failures, project isolation, Reaper reuse, retained volumes, cleanup diagnostics, and actual subprocess termination. The Linux verification run passed all 22 new cases plus three existing Ryuk tests. The five live integration cases skip native macOS because of Docker Desktop Ryuk instability.

A few lifecycle details for reviewers:

  • Generated names override COMPOSE_PROJECT_NAME and top-level Compose name; explicit resource names and host ports still require care.
  • keep_volumes=True preserves resources during normal context exit, but they remain eligible for Ryuk cleanup after disconnection.
  • Global Ryuk disabling takes precedence over registration.
  • Normal context-manager cleanup and Reaper’s existing interpreter-exit behavior are preserved. Ryuk remains a fallback for abrupt termination.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant