Is there a way to define commands to be executed before container exits

Is there a way we can define to execute certain commands that will be executed when a docker container exits.
Say for example, I would like to copy my GC logs placed inside my container into my Host or AWS S3 location before a container exits.

One more example I see is if in case of Out of Memory or any abrupt killing of docker container, we would like to execute certain commands like copying log files …

Thanks
Muthu