← Back to Dashboard
Verified Production Fix

[docker/cli] docker ps -f should return nonzero exit status for no matches

GH-docker/cli#6640 • Mar 07, 2026

### ROOT CAUSE The `docker ps` command returns an exit status of 0 even when no containers match the specified filter, making it difficult for scripts to distinguish between a successful run with no results and an actual error. ### CODE FIX To fix this, we'll modify the `docker ps` command to return a non-zero exit status when no containers are found after applying the filter.
bash
diff --git a/cli/commands/ps.go b/cli/commands/ps.go
index someindex..anotherindex 100644
--- a/cli/commands/ps.go
+++ b/cli/commands/ps.go
@@ -some_line, 0 +some_line, 9 @@ func main() {
 	psCmd.Flags().StringVarP(&filterOpts.Label, "label", "l", "", "Filter containers by label")
 
 	containers, err := psCmd.Runner().Run()
+	if err != nil {
+		os.Exit(1)
+	}
+	if len(containers) == 0 {
+		os.Exit(1)
+	}
 
 	// Existing code to print containers...
 }


**Note:** The exact code may vary, but the idea is to check if any containers are found after filtering. If none are found, the command should exit with a non-zero status.

Deploy with DigitalOcean

Use this fix in production instantly. Claim your $200 developer credit.

Get Started →