MYHOSPITALNOW.COM

Find Trusted Doctors & Hospitals

Discover healthcare providers, compare hospitals, and explore treatment options worldwide.

Free Treatment Quotes • Doctor Search • Hospital Comparison

Find Healthcare Options

www.myhospitalnow.com

Docker commands Guide – docker history with examples

Hereโ€™s a complete tutorial on docker history, covering what it does, examples, and use cases.


What is docker history?

docker history is a Docker command used to show the history of an image. It provides information about each layer in the image, including the creation time, size, command used, and author.

Key Features:

  • Displays the layer-by-layer history of a Docker image.
  • Helps in troubleshooting, auditing, and optimizing image builds.
  • Useful for identifying image size contributors and build steps.

Basic Syntax

docker history [OPTIONS] IMAGE
Code language: CSS (css)

Options:

  • --no-trunc: Show the full output without truncating commands or IDs.
  • --quiet, -q: Show only image IDs.
  • --format: Format the output using Go templates (e.g., {{.CreatedBy}}).

Examples of docker history

1. Show History of an Image

docker history nginx:latest
Code language: CSS (css)

Example Output:

IMAGE          CREATED        CREATED BY                                      SIZE      COMMENT
e21c333399e4   1 hour ago     /bin/sh -c apt-get update && apt-get install   45.2MB    Added dependencies
<missing>      1 day ago      /bin/sh -c #(nop) CMD ["nginx" "-g" "daemonโ€ฆ   0B
<missing>      1 day ago      /bin/sh -c #(nop) EXPOSE 80                   0B
Code language: HTML, XML (xml)

2. Show Full Command Information (--no-trunc)

docker history nginx:latest --no-trunc
Code language: CSS (css)

This displays the full command and image ID without truncation.


3. Show Only Image IDs (--quiet)

docker history -q nginx:latest
Code language: CSS (css)

Example Output:

e21c333399e4
<missing>
<missing>
Code language: HTML, XML (xml)

4. Use Custom Formatting

docker history --format "Created: {{.CreatedSince}}, Size: {{.Size}}, Command: {{.CreatedBy}}" nginx:latest
Code language: JavaScript (javascript)

Example Output:

Created: 1 hour ago, Size: 45.2MB, Command: /bin/sh -c apt-get update
Created: 1 day ago, Size: 0B, Command: /bin/sh -c #(nop) CMD ["nginx" "-g"โ€ฆ
Code language: JavaScript (javascript)

5. Show History for a Specific Image Tag

docker history my_app:1.0
Code language: CSS (css)

This shows the history of my_app with the 1.0 tag.


6. Compare History of Two Images

docker history my_app:1.0
docker history my_app:2.0
Code language: CSS (css)

This lets you compare layers to see what has changed between two versions.


7. Inspect Base Image Layers

docker history python:3.9
Code language: CSS (css)

Check the history of the python:3.9 image to see how it was built.


8. Use in a Script for Audit Logs

#!/bin/bash
docker history my_image --format "Layer: {{.ID}}, Size: {{.Size}}, Command: {{.CreatedBy}}" > history_log.txt
echo "Image history saved to history_log.txt"
Code language: PHP (php)

Use Cases for docker history

1. Auditing and Security

  • Audit Docker images to see how they were built and identify suspicious layers.
  • Example: Detect if unnecessary or malicious commands were executed during image creation.

2. Optimizing Docker Images

  • Identify large layers that contribute to the image size and optimize them.
  • Example: Consolidate RUN commands to reduce image layers.

3. Debugging Build Issues

  • Check the history to trace build errors and understand how each layer was created.
  • Example: Find out which layer failed during the build and fix the corresponding step in the Dockerfile.

4. Comparing Image Versions

  • Compare the history of different image tags to see what has changed between versions.
  • Example: Ensure that production images do not contain development tools or debug layers.

5. Understanding Base Images

  • Inspect the history of a base image to learn about its structure and configuration.
  • Example: Check which packages are installed in an official alpine or ubuntu image.

6. Automating Documentation and Audits

  • Use docker history in automation scripts to log and document image changes.

List of Common docker history Commands

CommandDescription
docker history nginx:latestShow the history of the nginx:latest image
docker history my_app:1.0Show history for a specific image tag
docker history --no-trunc my_app:1.0Show full command and image IDs
docker history --format "{{.CreatedBy}}" my_appShow only the commands used in each layer
docker history -q nginx:latestShow only the image IDs for each layer
docker history python:3.9Inspect the history of the python:3.9 base image
docker history --format "Size: {{.Size}}" my_appShow only the size of each layer

Best Practices for Using docker history:

  1. Regularly audit image history for security and compliance.
  2. Use custom formatting (--format) to filter and display relevant information.
  3. Optimize image size by identifying large layers and consolidating commands in your Dockerfile.
  4. Document image changes by saving the history to a file and including it in your CI/CD pipelines.
  5. Compare different image versions to track changes over time.

Common Errors and Solutions

  1. “No such image”
    โ†’ Ensure the image exists by running docker images to list available images.
  2. “Output truncated”
    โ†’ Use --no-trunc to display the full output without truncation.
  3. Large output size
    โ†’ Use --format to filter and customize the output for easier readability.

Combining docker history with Other Commands

Analyze Image Size and Optimize Layers

docker history my_app:latest --format "Layer: {{.ID}}, Size: {{.Size}}, Command: {{.CreatedBy}}"
Code language: JavaScript (javascript)

Save Image History for Documentation

docker history my_app:latest > history_log.txt
Code language: CSS (css)

Identify Suspicious Commands in an Image

docker history my_app | grep "curl" | grep "wget"
Code language: JavaScript (javascript)

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services โ€” all in one place.

Explore Hospitals
I'm Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms. I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.

Related Posts

Install Sonatype Nexus with Docker: RM2/RM3 Commands, Validation Steps, and Important Caveats

A structured, production-minded rewrite of the original 2016 Docker setup for Sonatype Nexus Repository Manager 2 and 3. Includes command examples, a workflow diagram, and clear warnings about outdated tags/endpoints.

Read More

Docker Lab & Assignment – Creating an Image

Hereโ€™s a detailed Tutorial and Lab Guide for the listed Docker commands, focused on practical usage, explanations, and step-by-step labs with Ubuntu + git + apache2 as…

Read More

Docker commands Guide – docker tag with examples

Hereโ€™s a complete tutorial on docker tag, covering what it does, examples, and use cases. What is docker tag? docker tag is a Docker command that creates…

Read More

Docker commands Guide – docker save with examples

Hereโ€™s a complete tutorial on docker save and docker load, covering what they do, examples, and use cases. What are docker save and docker load? Key Features:…

Read More

Docker commands Guide – docker rmi with examples

Hereโ€™s a complete tutorial on docker rmi, including what it does, examples, and use cases. What is docker rmi? docker rmi is a Docker command used to…

Read More

Docker commands Guide – docker load with examples

Hereโ€™s a complete tutorial on docker load, covering what it does, examples, and use cases. What is docker load? docker load is a Docker command used to…

Read More
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
0
Would love your thoughts, please comment.x
()
x