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:

  • Accessing tag as an environment variable inside a Docker container
  • pthreads process-shared memory when using: docker run --ipc --pid
  • APK is not installing in docker emulator
  • How to run GPGPU inside docker image with different from host kernel and GPU driver version
  • Docker Hub Automated Build - Tagging
  • Docker Compose when to use image over build
  • 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.

  • Container Orchestration for provisioning single containers based on user action
  • Multiple dokku apps one domain
  • “Default” docker machine does not exist on Linux when Docker daemon is running
  • How to run install pip requirements from private repo in google cloud source?
  • Decouple and Dockerize Django and Celery
  • What's the reason for the 42 layer limit in Docker?
  • Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.