Skip to content

docker ps -f should return nonzero exit status for no matches #6640

@damobrisbane

Description

@damobrisbane

Description

As per vdemeester comment, re-opening moby/moby#35057.

Reason to close was command will return zero even when "docker ps" returns no containers.

If instead docker ps returns ie 1 or not zero when container is not running, it then enables better integration with other tooling:

# Conventional Exit Code Use - ls command on folder exists, not-exists
 
if [[ $( ls /tmp 2>/dev/null ) ]]; then echo "folder exists"; else echo "folder does not exist"; fi

folder exists
 
if [[ $( ls /tmpXX 2>/dev/null ) ]]; then echo "folder exists"; else echo "folder does not exist"; fi

folder does not exist

# Docker ps - Less Useful Zero Exit Code

if [[ $( docker ps --filter label=runningimage=no 2>/dev/null ) ]]; then echo "container is running"; else echo "container is not running"; fi

container is running

??? WHY
 
eg, docker ps - no containers are running
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
.... 


Another closer example, "pgrep" - listing processes by process matching, clearly explains rationale:

...
       -c, --count
              Suppress  normal  output;  instead print a count of matching processes.  When count does not match anything, e.g. returns zero, the command will return
              non-zero value. Note that for pkill and pidwait, the count is the number of matching processes, not the processes that were  successfully  signaled  or
              waited for.


Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions