How to make docker container ls -f name filter by exact name?

There's only one container named exactly "foo", so use:

$ docker container inspect foo

instead, and you can format the output to get whatever data you need from that. E.g. to show the current status and image:

$ docker container inspect foo -f '{{.State.Status}} {{.Config.Image}}'
running foo_image:latest

From the comments, to see if the container "foo" exists, you can use:

if docker container inspect foo >/dev/null 2>&1; then
  echo "container exists"
else
  echo "container does not exit"
fi

docker container ls -f name=^/foo$

It can be achieved by using a regular expression. The caret ^ matches the exact start of the name and the dollar $ matches the end.

The slash / is needed because the filter mechanism expects the name prefixed with a slash.

Tags:

Docker