Run Clean-up Logic on Docker Container's Shutdown/Killing?


  • Host machine
  • Docker container running web service

I’d like to implement “clean-up” logic upon the shutdown of the web service. It seems to me that there’s 2 ways to do this:

  • Docker and high performance cluster (HPC) ability
  • exec is no use ,I do not know how to solve
  • Cron in the same container works locally but fails in cluster
  • Following the Get Started: Stack server stuck loading page endlessly
  • Excluding all hidden files with .dockerignore
  • difference between localhost and postgres for host in docker
  • 1. [On Dockerfile’s ENTRYPOINT] Use trap per the docs

    trap "echo TRAPed signal" HUP INT QUIT KILL TERM
    /usr/sbin/apachectl start

    2. [On Host] Use docker events

    Run docker events, filtering on die or shutdown (see docs) until I see my container’s hash. Then run my clean-up logic when such an event occurs (example: via bash script`)


    I know little about Linux’s SIGNAL‘s, as well as how processes handle such SIGNALs. So, I’m not sure if it’s possible in some scenarios for the clean-up logic to not be called.

    In short, I’m curious of the best way to ensure that my clean-up script runs upon the shutdown/killing of a docker container.

  • Removing a file from a docker container
  • How to edit ENTRYPOINT file of a docker image.
  • .Net Core Machine Key alternative for webfarm
  • docker-compose where can I get a detail log (info) about what happened
  • Dockerfile - Angular2
  • Commit a docker container but doesn't have any changes I made
  • Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.