Skip to content

Format Command

Docker uses Go templates which you can use to manipulate the output format of certain commands and log drivers.

Docker provides a set of basic functions to manipulate template elements. All of these examples use the docker inspect command, but many other CLI commands have a --format flag, and many of the CLI command references include examples of customizing the output format.

Whe using the --format flag, you need observe your shell environment. In a Posix shell, you can run the following with a single quote:

docker inspect --format '{{join .Args " , "}}' container
Otherwise, in a Windows shell (for example, PowerShell), you need to use single quotes, but escape the double quotes inside the params as follows:
docker inspect --format '{{join .Args \" , \"}}' container

To find out what data can be printed, show all content as json:

docker container ls --format='{{json .}}'

join

join concatenates a list of strings to create a single string. It puts a separator between each element in the list.

docker inspect --format '{{join .Args " , "}}' container

table

table specifies which fields you want to see its output.

docker image list --format "table {{.ID}}\t{{.Repository}}\t{{.Tag}}\t{{.Size}}"

json

json encodes an element as a json string.

docker inspect --format '{{json .Mounts}}' container

lower

lower transforms a string into its lowercase representation.

docker inspect --format "{{lower .Name}}" container

split

split slices a string into a list of strings separated by a separator.

docker inspect --format '{{split .Image ":"}}'

title

title capitalizes the first character of a string.

docker inspect --format "{{title .Name}}" container

upper

upper transforms a string into its uppercase representation.

docker inspect --format "{{upper .Name}}" container

println

println prints each value on a new line.

docker inspect --format='{{range .NetworkSettings.Networks}}{{println .IPAddress}}{{end}}' container

Exercise

  1. print the number of containers:

    There are 32 containers
    

  2. print the container-id, image, command and status using table format:

    CONTAINER ID        IMAGE                             COMMAND                  STATUS
    b213c64c192a        busybox                           "sh -c 'while true; …"   Exited (255) 23 minutes ago
    970631001c64        busybox                           "sh -c"                  Exited (2) 7 days ago
    

  3. print the CPU, Memory, Network IO, and Name, using table format:

    CPU %               MEM %               NET I/O             NAME
    0.00%               0.04%               1.03kB / 0B         checktime