moby-ryuk icon indicating copy to clipboard operation
moby-ryuk copied to clipboard

Allow to register shutdown hooks for containers terminated by Ryuk

Open shelajev opened this issue 1 year ago • 2 comments

Sometimes containers need to execute some shut down logic. In the code I can override a lifecycle method, something like containerIsStopping in Testcontainers-java for example and execute custom shutdown logic.

But if my containers are garbage collected by Ryuk, it's currently impossible (or very hard) to provide a shutdown logic.

I'd like to be able to register a shutdown hook for Ryuk, it can be as simple as enabling the behavior and Ryuk checking for a particular file path and running that file if exists before killing the container.

This will allow me:

  • enable shutdown hooks in Ryuk
  • copy the script with the shutdown logic into the container
  • override the containerIsStopping with calling the same file in the container

And my containers will clean up resources whether they are stopped by code or Ryuk.

shelajev avatar Aug 09 '24 12:08 shelajev

Sometimes containers need to execute some shut down logic

Do you have examples?

But if my containers are garbage collected by Ryuk, it's currently impossible (or very hard) to provide a shutdown logic.

The idea is that Ryuk should kill containers, not gracefully stop them. Any graceful shutdown should be done explicitly in the tests. But maybe I am missing some valid use case?

bsideup avatar Aug 09 '24 13:08 bsideup

@shelajev in tc-go there are PreTerminates and PostTerminate hooks, which are called in the Go test program. So if you are used to using ctr.Terminate(ctx) in your Go tests, then you benefit from using those hooks.

I agree with @bsideup here, as Ryuk is just the messenger 🤷

mdelapenya avatar Sep 02 '24 11:09 mdelapenya

@shelajev I'm closing this as stale. You can still leverage the container lifecycle to execute those hooks. But please reopen if you still this as a valid use case.

Thanks!

mdelapenya avatar Mar 14 '25 10:03 mdelapenya