How to execute a script when I terminate a Docker container?


Introduction

Executing a script upon container termination is the process of running a script or command when a Docker container is stopped or removed. This can be useful in a variety of scenarios, such as cleaning up resources, logging information, or triggering other actions.

Container termination refers to the process of stopping or removing a Docker container. This can be done manually using the docker stop or docker rm commands, or automatically through the use of container orchestration tools or container lifecycle management features.

Example 1: ONBUILD

To execute a script upon container termination using the ONBUILD Dockerfile command, follow these steps −

  • Create a Dockerfile for the image that will be used as the base for the container. In the Dockerfile, use the ONBUILD command to specify the script or command that you want to run upon container termination.

FROM alpine:3.11

ONBUILD RUN echo "Container terminated" >> /tmp/terminated.log
  • Build the image using the docker build command. Replace <image_name> with the desired name for the image.

$ docker build -t <image_name> .
  • Create a Dockerfile for the container that will be based on the image containing the ONBUILD command. In the Dockerfile, use the FROM command to specify the image containing the ONBUILD command as the base image.

FROM <image_name> 
  • Build the container using the docker build command. Replace <container_name> with the desired name for the container.

$ docker build -t <container_name> . 
  • Run the container using the docker run command. Replace <container_name> with the name of the container you built in the previous step.

$ docker run <container_name> 

When you stop or remove the container, the script or command specified in the ONBUILD command will be executed. In this example, the script will write the message "Container terminated" to the file /tmp/terminated.log.

Example 2: HEALTHCHECK

To execute a script upon container termination using the HEALTHCHECK Dockerfile command, follow these steps −

  • Create a Dockerfile for the image that will be used as the base for the container. In the Dockerfile, use the HEALTHCHECK command to specify a script or command that will be executed periodically to check the health of the container. The script or command should be designed to detect when the container has stopped or been removed, and exit with a non-zero status code if this is the case.

FROM alpine:3.11

HEALTHCHECK --interval=5s --timeout=3s \
  CMD [ -f /tmp/terminated ] || exit 1
  • Build the image using the docker build command. Replace <image_name> with the desired name for the image.

$ docker build -t <image_name> . 
  • Create a Dockerfile for the container that will be based on the image containing the HEALTHCHECK command. In the Dockerfile, use the FROM command to specify the image containing the HEALTHCHECK command as the base image.

FROM <image_name> 
  • Build the container using the docker build command. Replace <container_name> with the desired name for the container.

$ docker build -t <container_name> .
  • Run the container using the docker run command. Replace <container_name> with the name of the container you built in the previous step.

$ docker run <container_name> 

When you stop or remove the container, the script or command specified in the HEALTHCHECK command will be executed periodically. If the script or command exits with a non-zero status code, the container will be considered unhealthy and will be stopped or removed. In this example, the script will check for the existence of the file /tmp/terminated, and exit with a non-zero status code if the file exists.

Example 3: The --init flag

To execute a script upon container termination using the --init flag, follow these steps −

  • Create a script or command that you want to run upon container termination. The script or command should be designed to run as an init system and should handle signals such as SIGTERM and SIGINT to detect when the container is being stopped or removed.

#!/bin/bash

echo "Starting init system"

trap "echo 'Received signal to terminate'; exit 0" SIGTERM SIGINT

while true; do
  sleep 1
done
  • Save the script or command to a file, and make it executable using the chmod command.

$ chmod +x myscript.sh 
  • Create a Dockerfile for the image that will be used as the base for the container. In the Dockerfile, use the COPY command to copy the script or command file to the container.

FROM alpine:3.11

COPY myscript.sh /usr/local/bin/myscript
  • Build the image using the docker build command. Replace <image_name> with the desired name for the image.

$ docker build -t <image_name> .
  • Run the container using the docker run command, with the --init flag to run the init system in the container. Replace <image_name> with the name of the image you built in the previous step, and <container_name> with the desired name for the container.

$ docker run --init --name <container_name> <image_name> 

When you stop or remove the container, the init system specified in the --init flag will be run. The init system will handle signals such as SIGTERM and SIGINT to detect when the container is being stopped or removed and will execute the appropriate actions. In this example, the init system will write the message "Received signal to terminate" to the console, and then exit with a status code of 0.

Conclusion

In this article, we looked at how to execute a script upon container termination. We saw that there are several options available for executing a script upon container termination, such as the ONBUILD and HEALTHCHECK Dockerfile commands, and the --init flag. We also saw examples of how to use each of these options and discussed some best practices for choosing the right option and optimizing the performance and reliability of the script execution.

Updated on: 17-Feb-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements