Sponsored Content
Skip to content

support PostExit Hook #1182

Open
Open

Description

@fahedouch

What is the problem I am trying to solve ?

Looking at the list of supported hooks, we are missing a hooks for configuring custom actions related to the exit lifecycle ( i.e normal exist / killing a container ..)

In nerdctl project, we need a hooks to deal with container deps when the container exit, such as cleaning some state files.

Describe the solution

add new hook PostExit to the hooks list

Activity

  1. AkihiroSuda commented on Feb 14, 2023

    @AkihiroSuda
    Member

    Did you mean "exist" or "exit" ?

  2. AkihiroSuda commented on Feb 14, 2023

    @AkihiroSuda
    Member

    And how would it be different from poststop hooks?

  3. fahedouch commented on Feb 14, 2023

    @fahedouch
    Author

    @AkihiroSuda

    Did you mean "exist" or "exit" ?

    Yes exit

    And how would it be different from poststop hooks?

    poststop hook is called after the container is deleted but before the delete operation returns.

    PostExit should be called after the container exit

  4. AkihiroSuda commented on Feb 14, 2023

    @AkihiroSuda
    Member

    PostExit should be called after the container exit

    How implementable?

  5. fahedouch commented on Feb 14, 2023

    @fahedouch
    Author

    PostExit should be called after the container exit

    How implementable?

    did not get your question ? You mean how to introduce a runtime implementation. If yes I believe that we can easily do this in runc for example, but I have to dig into before answering this question.

  6. AkihiroSuda commented on Feb 15, 2023

    @AkihiroSuda
    Member

    An OCI runtime process disappears on exec(2)-ing the container process, so I'm not sure how this proposal is implementable.

  7. fahedouch commented on Feb 15, 2023

    @fahedouch
    Author

    @AkihiroSuda would please give me more details about exec(2)-ing the container process or may be link code or article around this. I am looking for an entrypoint to understand what are saying. thanks

  8. AkihiroSuda commented on Feb 15, 2023

    @AkihiroSuda
    Member

    @AkihiroSuda would please give me more details about exec(2)-ing the container process or may be link code or article around this. I am looking for an entrypoint to understand what are saying. thanks

    https://github.com/opencontainers/runc/blob/v1.1.4/docs/terminals.md#detached

    In contrast to foreground mode, in detached mode there is no long-running
    foreground runc process once the container has started. In fact, there is no
    long-running runc process at all.

  9. fahedouch commented on Feb 21, 2023

    @fahedouch
    Author

    @AkihiroSuda would please give me more details about exec(2)-ing the container process or may be link code or article around this. I am looking for an entrypoint to understand what are saying. thanks

    https://github.com/opencontainers/runc/blob/v1.1.4/docs/terminals.md#detached

    In contrast to foreground mode, in detached mode there is no long-running
    foreground runc process once the container has started. In fact, there is no
    long-running runc process at all.

    got the point. Do we have a hook mechanism into containerd that may catch container exit ?

  10. AkihiroSuda commented on Feb 21, 2023

    @AkihiroSuda
    Member

    Do we have a hook mechanism into containerd that may catch container exit ?

    Feel free to submit a proposal in the containerd repo.
    Maybe we can extend its restart monitor plugin.

  11. fahedouch commented on Feb 25, 2023

    @fahedouch
    Author
  12. Elthis commented on Mar 3, 2026

    @Elthis

    @fahedouch

    Hmm, I've recently was going through containerd source code and noticed that they are monitoring if container exited:

    https://github.com/containerd/containerd/blob/f4681e01d0fab33ccb2a88dc9c255b5cb4eba1ea/internal/cri/server/events.go#L173

    So containerd has a way to monitors container exit and acts upon it (updates internal container status as stopped and fills the exit information).

    Maybe this would allow to push this issue further?

    IMHO postexit hook should work the same way the poststop hook does. But trigger on exit, not delete.

    This would be useful, since OCI container delete times differ between different CRI:
    See cri-o/cri-o#9732

  13. cyphar commented on Mar 3, 2026

    @cyphar
    Member

    There was a discussion about this topic many years ago (before 1.0), but I can't seem to find the discussion thread... EDIT: #926 is related but there was a longer thread where someone actually proposed a prototype, which is the thread I can't find.

    IMHO postexit hook should work the same way the poststop hook does. But trigger on exit, not delete.

    You appear to have a misunderstanding of how OCI runtimes work -- after the container is started, there is no guarantee that there is any monitor process from the OCI runtime that can catch the container exit. In runc's case, this happens when you run in detached mode (which how all the higher-level runtimes use runc). This means that there is no runc code that could detect the exit and spawn a hook!

    In contrast, poststop works because runc delete needs to be run in order to delete the container. (The name is unfortunately quite misleading -- almost all of the old hook names are.)

    Higher-level runtimes can detect exits because they implement their own process monitors (containerd has containerd-shim, podman has conmon, etc). runc does not have a built-in process monitor (unlike LXC) which means there is no way for us to run a hook at the instant a container dies.

    Back in the 2010s there were proposals to do this on a best-effort basis (for instance, the next time a runc command is called we detect all containers that died and run their post-stop hooks) but this has all sorts of awful issues (permissions, possibilities of a DoS, correctness) that is just doesn't make sense.

  14. Elthis commented on Mar 3, 2026

    @Elthis

    Aww, okay. I guess I understand the problem now.

    Thanks @cyphar for the descriptive answer.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions