All Docker container statuses?

2020-08-17 05:25发布

The list of Docker statuses is here. However, when I list docker containers using the API, the statuses are shown in 'natural' sentences; e.g.:

  • Exited (0) NN seconds ago
  • Up NN days
  • and so on...

I couldn't find the definitive list of all string outputs for all the statuses. In other words, I want to parse docker API status strings.

What are all the possible outputs of the Docker API for container statuses?

Here is the api Im talking about.

标签: docker
3条回答
冷血范
2楼-- · 2020-08-17 05:57

From their docs

One of created, restarting, running, removing, paused, exited, or dead

In my experience, immediately on starting a container, it's created, then running, then when it exits with a zero or non-zero exit code, it is exited.

查看更多
虎瘦雄心在
3楼-- · 2020-08-17 06:11

The logic by which the status summary is generated can be found in the Docker source code, in the file container/states.go, l. 41ff.. Basically, you'll get one of the following:

  • Up 1 day (paused)
  • Restarting (123) 1 day ago
  • Up 1 day
  • Removal in Progress
  • Dead
  • Created
  • Exited (123) 1 day ago
  • (empty string)

In order to get a machine-readable output, I'd suggest using the /containers/:id/json endpoint, which will return a data structure like the following:

"State": {
    "Dead": false, 
    "Error": "", 
    "ExitCode": 0, 
    "FinishedAt": "0001-01-01T00:00:00Z", 
    "OOMKilled": false, 
    "Paused": false, 
    "Pid": 2593, 
    "Restarting": false, 
    "Running": true, 
    "StartedAt": "2015-12-26T19:22:38.616937722Z", 
    "Status": "running"
}
查看更多
霸刀☆藐视天下
4楼-- · 2020-08-17 06:16

I've not used the remote API, but I'm pretty sure what you actually want to do is get the ID of all containers, then get the State information for each container using /containers/(id)/json:

...
  "State": {
        "Error": "",
        "ExitCode": 9,
        "FinishedAt": "2015-01-06T15:47:32.080254511Z",
        "OOMKilled": false,
        "Paused": false,
        "Pid": 0,
        "Restarting": false,
        "Running": true,
        "StartedAt": "2015-01-06T15:47:32.072697474Z",
        "Status": "running"
    },
... 

That way you get the same data in a much more standard form.

查看更多
登录 后发表回答