Sub topic 5
Full commands and custom formats
This chapter assumes you can build and run images, read a simple Dockerfile, and use a Linux shell. It covers the options that make docker history output easier to read and to process in scripts: --no-trunc, --format, and -q.
Why the default output falls short
By default, docker history cuts the CREATED BY column to about 45 characters. That is fine for a quick look. It hides the details when a RUN line chains several commands, which is the case you most need to inspect.
Build the wasteful image from earlier in this tutorial:
docker build -t demo:wasteful .
docker history demo:wastefulA long instruction appears as RUN /bin/sh -c apt-get update && apt-get i.... You cannot see whether a cleanup step was part of that layer.
Showing full instructions with --no-trunc
--no-trunc turns off truncation. It affects every column, so layer IDs also print in full, as sha256: followed by 64 hex characters.
docker history --no-trunc demo:wastefulUse it when you need to confirm what a layer ran. For example, you can check whether rm -rf /var/lib/apt/lists/* sits in the same instruction as apt-get install. If it is in a separate RUN, the files stay in the earlier layer.
The output is wide. Pipe it to less -S to scroll sideways instead of wrapping lines:
docker history --no-trunc demo:wasteful | less -SChoosing columns with --format
--format takes a Go template. You choose the columns and their order. The table prefix adds a header row and aligns the columns.
docker history --format "table {{.ID}}\t{{.Size}}\t{{.CreatedBy}}" demo:wastefulThe placeholders available are:
| Placeholder | Meaning |
|---|---|
| `{{.ID}}` | Layer image ID, or `<missing>` for layers not stored locally |
| `{{.CreatedBy}}` | The instruction that created the layer |
| `{{.CreatedSince}}` | How long ago the layer was created |
| `{{.CreatedAt}}` | Timestamp of creation |
| `{{.Size}}` | Size the layer adds |
| `{{.Comment}}` | Comment recorded at build time |
Without the table prefix, --format prints one line per layer with no header. That form suits scripts:
docker history --format "{{.Size}} {{.CreatedBy}}" demo:wastefulYou can also print JSON, one object per layer, with --format json. Tools such as jq can then filter it.
docker history --no-trunc --format json demo:wasteful | jq -r '.CreatedBy'Printing only IDs with -q
-q (or --quiet) prints only the layer IDs. It is mostly useful for counting layers:
docker history -q demo:wasteful | wc -lThe count includes <missing> entries, because they are still layers in the image history.
Combining the options
Options can be combined. A common pattern is to read the full instruction next to the size:
docker history --no-trunc --format "table {{.Size}}\t{{.CreatedBy}}" demo:wastefulNote that --no-trunc and a custom --format work together. The template controls which columns appear. --no-trunc controls whether each value is cut.
Common errors
- Table columns misaligned or missing header. You left off the
tableprefix in--format. Addtableat the start of the template. - Tabs printed literally. In some shells,
\tinside single quotes is passed as typed. Docker's template engine interprets\t, so double quotes or single quotes both work in most cases. If you see a literal\t, check how your shell quotes the argument. - Unknown placeholder. A name such as
{{.Command}}does not exist for history. The error message names the unsupported field. Use{{.CreatedBy}}. - Sizes that look wrong.
docker historyshows the size each layer adds. A0Blayer is normal for metadata instructions such asENVorCMD.
Exercise
You want a table showing only each layer's size and its full, untruncated instruction for the image demo:wasteful. Which command do you run, and why is --no-trunc needed even though you set --format?
Show answer
Run docker history --no-trunc --format "table {{.Size}}\t{{.CreatedBy}}" demo:wasteful. The --format template picks the columns, but it does not stop Docker from cutting long values. Without --no-trunc, the CreatedBy text is still shortened to about 45 characters.
Exercise
How can you count the layers in an image from the command line?
Show answer
Run docker history -q demo:wasteful | wc -l. The -q option prints one ID per line, and wc -l counts the lines. <missing> entries are included in the count.