如何获取docker restart命令下已停止进程状态及容器停止退出状态
docker restart Great question! When you run docker restart, Docker first sends a SIGTERM signal to the container's main process, waits a default 10 seconds for it to exit gracefully, and sends a SIGKILL if it doesn't shut down in time. Here's how you can pull the stop-phase exit status and details about the stopped process:
1. Get the Container's Stop Phase Exit Status
The most direct way to get the exit code from the stop phase is using docker inspect with a formatted output. Docker stores this exit code in the container's state metadata.
Fetch just the exit code:
Run this command, replacing<container-id-or-name>with your container's ID or name:docker inspect --format '{{.State.ExitCode}}' <container-id-or-name>- An exit code of
0means the process exited gracefully in response toSIGTERM. - A code of
137indicates the process was force-killed withSIGKILL(either because it didn't exit within the timeout, or was manually killed). - Other non-zero codes map to specific errors from the container's main process (check your application's documentation for what these mean).
- An exit code of
View full stop phase details:
For more context (like when the container stopped, if it was killed, or any error messages), run the fulldocker inspectcommand and look at theStatesection:docker inspect <container-id-or-name>Key fields to look for:
ExitCode: The exit code we mentioned earlier.StoppedAt: The timestamp when the container stopped.Error: If there was an error during the stop process, this field will contain details.Killed: A boolean (true/false) that tells you if the process was force-killed withSIGKILL.
2. Get Details About the Stopped Process
Once the container is stopped, the process no longer runs, but you can still retrieve context about its state before shutdown:
Check container logs:
Most applications log their shutdown process (like cleanup steps or error messages). Usedocker logsto view the end of the logs, which will show what the process was doing right before stopping:# View the last 50 lines of logs docker logs --tail 50 <container-id-or-name> # View logs from the last 10 minutes (covers the restart window) docker logs --since "10m" <container-id-or-name>This is especially useful if the process exited with a non-zero code—you’ll often find the root cause in the logs.
Monitor Docker events in real-time:
If you want to track the stop/start sequence as it happens, open a separate terminal and run:docker events --filter 'container=<container-id-or-name>'When you run
docker restart, you’ll see events likecontainer stopandcontainer startwith timestamps. Pair this with the logs to get a full picture of the shutdown process.Pre-restart process state (optional):
If you want to capture the process state right before the restart, run this command before executingdocker restart:docker top <container-id-or-name>This will show you the running processes inside the container, their PIDs, resource usage, and more—helpful if you suspect the process was in a stuck state before shutdown.
内容的提问来源于stack exchange,提问作者jaggu

