You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何获取docker restart命令下已停止进程状态及容器停止退出状态

How to Get Stop Phase Status and Exit Code When Using 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 0 means the process exited gracefully in response to SIGTERM.
    • A code of 137 indicates the process was force-killed with SIGKILL (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).
  • View full stop phase details:
    For more context (like when the container stopped, if it was killed, or any error messages), run the full docker inspect command and look at the State section:

    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 with SIGKILL.

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). Use docker logs to 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 like container stop and container start with 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 executing docker 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:14:34