This repository has been archived by the owner on Dec 19, 2024. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
When debugging containers, it's always a pain understanding why a fault occured. This is especially true since the last exec() call will make the container being stopped if the command fails. This commit is about creating a trick to catch errors and preventing the container from dying. This first implies adding some 'trap ERR' to catch errors but as per docker environment, PID 1 received the SIGTERM when 'docker stop' command occurs. So SIGTERM have to be forwarded to the exec'd process. The current implementation is split in two parts: - handling the ERR - handling exec + SIGTERM For the ERR, set_trap_err() does install the ERR trap and executes trap_error() if triggered. This function is just displaying information to the user and fall in sleep to keep the PID 1 alive for ever. As the container is in sleep, it is possible to spawn a new bash in it by using the 'docker exec' command. A special trick here, is that new bash will reimport the 'env' context of PID 1 by loading a modified ~/.bashrc. This file is also providing a override function of the built-in exec() call. It does install the SIGTERM trap, spawn the desired command in background and wait it to die before exiting with its return value. The spawned process is not PID 1 so the SIGTERM trap will forward it to the proper PID and then sucide itself with the appropriate return value. The beauty of that is that sourcing docker_exec.sh is enough to get all thoses features at once as this script is executing set_trap_err() by default and overrides the exec() built-in function. A typical usage looks like : if [ "$MY_DEBUG_CONDITION" = "what_ever_you_want" ]; then source docker_exec.sh fi Note: I'm not really happy about the NOTRAP variable to disengage the 'trap ERR' but I didn't found a way to remove it from the SIGTERM trap in an efficient way :/
- Loading branch information