Reputation: 982
docker stop
sends the SIGTERM
signal to the main process of a container. If the main process does not terminate after a default timeout period of 10 seconds, then the SIGKILL
signal is sent to forcefully terminate the process. docker stop
always returns 0, however, regardless if the process was terminated gracefully or not. This example, for instance, shows that the command returns 0 even though the process (sleep infinity
) was terminated forcefully (after 10 seconds of waiting):
[student@c7c ~]$ docker run -d --name test_container busybox sleep infinity
b52c8fce1d7aeb20782a0e2e5bf970911363632b84b1c01e6dbbb985440e8f8b
[student@c7c ~]$ docker stop test_container
test_container
[student@c7c ~]$ echo $?
0
My question is, how should I edit my command(s) to be able to stop a container and yet be able to determine if the process in the container was terminated gracefully or forcefully due to timeout.
Upvotes: 0
Views: 561
Reputation: 1176
You can use the Exit Status from the docker inspect command.
Use the following command:
docker inspect test_container --format='{{.State.ExitCode}}'
In case of graceful termination of the container, it will return 0
else a non-zero integer.
Upvotes: 1