Skip to content

enhancement: add reaper option - #949

Open
ClaytonNorthey92 wants to merge 1 commit into
testcontainers:mainfrom
ClaytonNorthey92:clayton/reaper-container
Open

enhancement: add reaper option#949
ClaytonNorthey92 wants to merge 1 commit into
testcontainers:mainfrom
ClaytonNorthey92:clayton/reaper-container

Conversation

@ClaytonNorthey92

@ClaytonNorthey92 ClaytonNorthey92 commented Jun 3, 2026

Copy link
Copy Markdown
  • add testcontainers/ryuk as a reaper option
  • have this OFF by default, can be set with .with_reaper(true)
  • add a timeout for the reaper container that represents when a reaper container should stop and stop others
  • add no_drop option for not stopping a container when Drop is called, this is used to ensure the reaper container isn't stopped when it leaves the start() scope

fixes #577

@netlify

netlify Bot commented Jun 3, 2026

Copy link
Copy Markdown

Deploy Preview for testcontainers-rust ready!

Name Link
🔨 Latest commit 793b348
🔍 Latest deploy log https://app.netlify.com/projects/testcontainers-rust/deploys/6a3311a5d001f200088ecb24
😎 Deploy Preview https://deploy-preview-949--testcontainers-rust.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@ClaytonNorthey92
ClaytonNorthey92 force-pushed the clayton/reaper-container branch from 9f4f50b to 00cb21d Compare June 3, 2026 12:28
@ClaytonNorthey92
ClaytonNorthey92 force-pushed the clayton/reaper-container branch 3 times, most recently from a1a05e8 to e0769a2 Compare June 9, 2026 18:02

@DDtKey DDtKey left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution!

I'm sorry for the delay with review process, my bandwidth is partially limited atm.

However I'll dedicate time this week

Meanwhile, @codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: e0769a251e

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread testcontainers/src/core/image.rs Outdated
Comment on lines +111 to +113
fn reaper(&self) -> bool;

fn reaper_ttl_seconds(&self) -> u32;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Add defaults for the new Image methods

Adding these as required methods breaks every existing downstream Image implementation, and it also leaves the repo's own ExecBeforeReady test image under the http_wait_plain feature without implementations, so all-features builds fail. Since the behavior already has obvious defaults (false and the default Ryuk timeout), make these default trait methods instead of requiring every image to add boilerplate.

Useful? React with 👍 / 👎.


let host_port = reaper_container.get_host_port_ipv4(8080).await?;

let mut stream = std::net::TcpStream::connect(format!("127.0.0.1:{}", host_port))?;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Use the resolved Docker host for Ryuk

When the Docker daemon is remote, configured via tc.host/DOCKER_HOST=tcp://..., or the tests run inside a container, mapped ports are not necessarily reachable on the test process's 127.0.0.1; the rest of the crate handles this through get_host()/docker_hostname(). With this hard-coded address, with_reaper(true) fails to connect to a successfully-started Ryuk container in those supported environments, so use the reaper container's resolved host with the mapped port instead.

Useful? React with 👍 / 👎.

Comment on lines +418 to +420
.with_mount(Mount::bind_mount(
"/var/run/docker.sock",
"/var/run/docker.sock",

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Mount the configured Docker socket into Ryuk

This hard-codes /var/run/docker.sock, but the client explicitly supports rootless and other Unix socket locations via Docker host resolution. In those supported setups, the Ryuk container either gets a missing/wrong socket or fails to start, so any reaper-enabled container cannot be registered; derive the bind source from the configured Unix Docker host instead of assuming the default path.

Useful? React with 👍 / 👎.

.with_env_var("RYUK_CONNECTION_TIMEOUT", seconds_formatted.clone())
.with_env_var("RYUK_RECONNECTION_TIMEOUT", seconds_formatted.clone());

let mut reaper_container = reaper_image.start().await.unwrap();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Propagate Ryuk startup failures instead of panicking

When users opt into the reaper and Ryuk cannot be pulled or started (for example because the socket bind is rejected or the image is unavailable), this unwrap() panics inside start() even though the public API returns Result. That prevents callers from handling the startup failure consistently with other container setup errors; propagate the error with ? instead.

Useful? React with 👍 / 👎.

@ClaytonNorthey92
ClaytonNorthey92 force-pushed the clayton/reaper-container branch 5 times, most recently from e2acb33 to 716b69a Compare June 17, 2026 21:28
* add testcontainers/ryuk as a reaper option
* have this OFF by default, can be set with <image>.with_reaper(true)
* add a timeout for the reaper container that represents when a reaper container should stop and stop others
* add no_drop option for not stopping a container when Drop is called, this is used to ensure the reaper container isn't stopped when it leaves the start() scope

fixes testcontainers#577
@ClaytonNorthey92
ClaytonNorthey92 force-pushed the clayton/reaper-container branch from 716b69a to 793b348 Compare June 17, 2026 21:29
@mervyn-mccreight

mervyn-mccreight commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

Thanks for the contribution!

I wonder if it would be a good idea to have this as a feature (non-default).

@ClaytonNorthey92

ClaytonNorthey92 commented Jun 18, 2026

Copy link
Copy Markdown
Author

Thanks for the contribution!

I wonder if it would be a good idea to have this as a feature (non-default).

Happy to help!

The way I wrote it, it is off by default. If you run a container the reaper will not run unless you explicitly tell it to. It's not a "feature" turned off/on at compile time; it's a setting in the image instance at runtime. What do you think about that?

edit: tagging @mervyn-mccreight

@mervyn-mccreight

Copy link
Copy Markdown
Contributor

Hey :)

To me this is semantically roughly the same feature as the existing "watchdog" feature, but with another implementation. It tries to shut down containers that somehow life after everything is finished.

I'm asking myself:

Is there a use-case I can image that I want ryuk just for certain containers and not having it being the Fail-Safe for everything when I actively want to activate it? Or is it more like I either want it to be active in general or I want it to be not active in general?

At the moment I view it more like someone I want to either be active in general or not active in general, but I may be missing thoughts.

What do you think @DDtKey ?

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.

Support testcontainers/ryuk as resource reaper

3 participants