Print only `Names` column from `docker-container ls -la` output

When issuing the docker container ls -la command, the output looks like this:

CONTAINER ID   IMAGE     COMMAND               CREATED          STATUS          PORTS     NAMES
a67f0c2b1769   busybox   "tail -f /dev/null"   26 seconds ago   Up 25 seconds             recursing_liskov

I'd like to get only the container's name present in the Names column.

My idea was to use bash to reverse the columns in the command output so as to get the Names column in first position and cut the name of container.

This is what I tried:

sudo docker container ls -la | rev |tail -n +2 | tr -s ' ' |  cut -d ' ' -f 1

However this does not give the expected result and I get the following error:

rev: stdin: Invalid or incomplete multibyte or wide character

I'm stuck as I don't know how to handle this error. Can it be fixed to obtain the result I expect ? Or is there any other way to obtain my result ?


Rather than playing around with a default output, just print exactly what you are looking for from start. Most docker sub-commands accept a --format option which will take a go template expression to specify what you exactly want.

In your case, I believe the following command should give what you are looking for:

$ docker container ls -la --format "{{.Names}}"
recursing_liskov

Of course, you can add more columns if you wish, in whatever order best suits your needs. You can easily get a list of all keys available with something like:

$ docker container ls -la --format "{{json .}}" | jq
{
  "Command": "\"tail -f /dev/null\"",
  "CreatedAt": "2022-01-15 23:43:54 +0100 CET",
  "ID": "a67f0c2b1769",
  "Image": "busybox",
  "Labels": "",
  "LocalVolumes": "0",
  "Mounts": "",
  "Names": "recursing_liskov",
  "Networks": "bridge",
  "Ports": "",
  "RunningFor": "20 minutes ago",
  "Size": "0B (virtual 1.24MB)",
  "State": "running",
  "Status": "Up 20 minutes"
}

Here is an example just to illustrate using some of the fields:

$ docker container ls -la --format "container {{.Names}} is in state {{.State}} and has ID {{.ID}}"
container recursing_liskov is in state running and has ID a67f0c2b1769

Some random references out of several I used:

  • https://devcoops.com/filter-output-of-docker-image-ls/
  • https://windsock.io/customising-docker-cli-output/