Filter Docker Containers by Status or Exit Code
List containers by state with docker ps -a --filter status=exited, or by exit code with exited=3. Filters with different keys combine as AND, equal keys as OR.
Many containers exist and you want only the ones that stopped, or only those that failed.
docker run --name fq-ok alpine:3.20 true
docker run --name fq-bad alpine:3.20 sh -c 'exit 3'
docker run -d --name fq-up alpine:3.20 sleep 60 > /dev/null
docker ps -a --filter name=fq- --filter status=exited --format '{{.Names}}: {{.Status}}' | sort
echo "--- exit code"
docker ps -a --filter name=fq- --filter exited=3 --format '{{.Names}}'
echo "--- two statuses are OR"
docker ps -a --filter name=fq- --filter status=exited --filter status=running --format '{{.Names}}' | sort
docker rm -f fq-ok fq-bad fq-up > /dev/nullfq-bad: Exited (3) Less than a second ago
fq-ok: Exited (0) Less than a second ago
--- exit code
fq-bad
--- two statuses are OR
fq-bad
fq-ok
fq-up--filter status=exited shows containers that have stopped, and exited=3 selects those that ended with exit code 3, which found only fq-bad. Filters with different keys must all match, so the name filter and the status filter narrowed the list together. Repeating one key works as OR, and status=exited with status=running returned all three containers. See docker container ls.
exited works only for containers that have stopped, and the --filter name= form is a substring match, not an exact one, so anchor it with a regular expression such as ^fq-ok$ when names overlap. The age shown in the Status column changes from run to run.