Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if docker container is stopped or failed

Tags:

docker

I am attempting to check if (and handle all edge cases for) a container has been stopped or exited in an unclean state. I am using the 'State' block returned by docker inspect <container> to attempt to resolve this.

    "State": {
        "Status": "exited",
        "Running": false,
        "Paused": false,
        "Restarting": false,
        "OOMKilled": false,
        "Dead": false,
        "Pid": 0,
        "ExitCode": 0,
        "Error": "",
        "StartedAt": "2018-03-01T18:56:19.541980678Z",
        "FinishedAt": "2018-03-01T18:56:24.618264625Z"
    },

I know the 'ExitCode' of a stopped container will be 137, but there's a lot of other information there. Will filtering on State.ExitCode == 137 be enough to filter for stopped instances?

EDIT: I should mention the reason why I am attempting to do this instead of using pause and unpause to manage my containers is that I want and active/standby arrangement of containers with port bindings. A container in paused state still maintains its port bindings which I need released when it is in standby state.

like image 864
asdf Avatar asked Sep 14 '25 15:09

asdf


1 Answers

To get stopped container:

docker ps -f status=exited -f name=$container_name

See docker ps filtering documentation.

like image 90
Kert Kukk Avatar answered Sep 16 '25 08:09

Kert Kukk