---
title: "Inspecting Image Lineage with docker history"
date: "2026-11-18T14:30:00+07:00"
description: "How to inspect container layer origins, command instructions, size allocations, and security risks using docker history and diving tools."
author: "AI Assistant"
image: "https://blog.redlinesoft.net/images/post/inspecting-image-lineage-docker-history.jpg"
alt: "Terminal display showing docker history layer breakdown with sizes and build commands"
tags: ['docker', 'cli', 'debugging', 'image-building']
---

Container images are composed of stacked read-only layers. When optimizing image size or auditing third-party images for security vulnerabilities, you need to see **which Dockerfile instructions created each layer** and **how much disk space each layer consumes**.

The `docker history` command provides a detailed audit trail of an image's construction history layer by layer.

In this tutorial, we will learn how to use `docker history`, format output for analysis, identify bloated image layers, and audit hidden instructions.

## Basic Usage of `docker history`

To inspect the layer lineage of a local container image:

```bash
docker history nginx:1.25
```

Sample output:
```text
IMAGE          CREATED        CREATED BY                                      SIZE      COMMENT
92b11f4d1571   2 days ago     /bin/sh -c #(nop)  CMD ["nginx" "-g" "daemon…   0B        
<missing>      2 days ago     /bin/sh -c #(nop)  EXPOSE 80                    0B        
<missing>      2 days ago     /bin/sh -c apt-get update && apt-get install…   64.2MB    
<missing>      2 days ago     /bin/sh -c #(nop) COPY file:a7b8... in /etc/…   3.1kB     
<missing>      2 weeks ago    /bin/sh -c #(nop) ADD file:e91f... in /      74.8MB    
```

### Table Columns Breakdown:
- **IMAGE**: The layer ID hash (or `<missing>` for BuildKit/remote intermediate layers where layer IDs are hidden for cache optimization).
- **CREATED**: Relative timestamp when the layer was built.
- **CREATED BY**: The exact Dockerfile instruction (`RUN`, `COPY`, `ADD`, `ENV`) executed to create that layer.
- **SIZE**: Disk space added by that specific layer.

## Preventing Truncated Output with `--no-trunc`

By default, Docker truncates long `CREATED BY` instructions. To view full build commands (essential for security auditing and debugging complex `RUN` pipelines):

```bash
docker history --no-trunc nginx:1.25
```

## Formatting Output for Script Analysis

You can pass the `--format` flag to extract specific fields or generate clean reports:

```bash
# Print layer size and build instruction in tabular columns
docker history --format "table {{.Size}}\t{{.CreatedBy}}" node:20-slim
```

Sample output:
```text
SIZE        CREATED BY
0B          /bin/sh -c #(nop)  CMD ["node"]
0B          /bin/sh -c #(nop)  ENTRYPOINT ["docker-entrypoint.sh"]
7.8MB       /bin/sh -c apt-get update && apt-get install -y curl ...
200MB       /bin/sh -c #(nop) ADD file:7a8b9c... in / 
```

## Identifying Image Bloat and Security Risks

Using `docker history` helps engineers spot three common Dockerfile mistakes:

### 1. Temporary Build Dependencies Left in Layers
If you see a `RUN apt-get install gcc` layer adding 300 MB, followed by a later layer `RUN apt-get remove gcc` showing 0B, `docker history` proves that the 300 MB was permanently written into the earlier immutable layer!

**Fix**: Combine package installation, compilation, and cleanup into a single `RUN` layer, or use multi-stage builds.

### 2. Leaked Secrets in `ENV` or `ARG` Instructions
`docker history` displays metadata instructions like `ENV API_KEY=secret123` even if the container is running without environment variables. Anyone who pulls your image can run `docker history --no-trunc` to read build arguments!

**Fix**: Use BuildKit secret mounts (`RUN --mount=type=secret`) instead of `ARG` or `ENV` for sensitive build credentials.

### 3. Inefficient Layer Ordering
If large static files are added early in the history, changing a single config line invalidates layer caching for all subsequent layers.

## Beyond CLI: Deep Inspection with `dive`

For interactive, visual inspection of container layer contents, many DevOps engineers use **`dive`**, an open-source terminal tool:

```bash
# Install and run dive on an image
dive nginx:1.25
```

`dive` opens a split-pane interface showing each layer alongside the exact file tree changes (added, modified, deleted files) produced by that layer!

## Summary

`docker history` is an essential diagnostic command for auditing image lineage, uncovering size bloat, and preventing credential leaks in container builds.
