Find the Best Cosmetic Hospitals

Explore trusted cosmetic hospitals and make a confident choice for your transformation.

โ€œInvest in yourself โ€” your confidence is always worth it.โ€

Explore Cosmetic Hospitals

Start your journey today โ€” compare options in one place.

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 a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I have worked at <a href="https://www.cotocus.com/">Cotocus</a>. I share tech blog at <a href="https://www.devopsschool.com/">DevOps School</a>, travel stories at <a href="https://www.holidaylandmark.com/">Holiday Landmark</a>, stock market tips at <a href="https://www.stocksmantra.in/">Stocks Mantra</a>, health and fitness guidance at <a href="https://www.mymedicplus.com/">My Medic Plus</a>, product reviews at <a href="https://www.truereviewnow.com/">TrueReviewNow</a> , and SEO strategies at <a href="https://www.wizbrand.com/">Wizbrand.</a> Do you want to learn <a href="https://www.quantumuting.com/">Quantum Computing</a>? <strong>Please find my social handles as below;</strong> <a href="https://www.rajeshkumar.xyz/">Rajesh Kumar Personal Website</a> <a href="https://www.youtube.com/TheDevOpsSchool">Rajesh Kumar at YOUTUBE</a> <a href="https://www.instagram.com/rajeshkumarin">Rajesh Kumar at INSTAGRAM</a> <a href="https://x.com/RajeshKumarIn">Rajesh Kumar at X</a> <a href="https://www.facebook.com/RajeshKumarLog">Rajesh Kumar at FACEBOOK</a> <a href="https://www.linkedin.com/in/rajeshkumarin/">Rajesh Kumar at LINKEDIN</a> <a href="https://www.wizbrand.com/rajeshkumar">Rajesh Kumar at WIZBRAND</a> <a href="https://www.rajeshkumar.xyz/dailylogs">Rajesh Kumar DailyLogs</a>

Related Posts

Docker Tutorials: Docker Image – Understanding Dockerfiles instructions & options

Hereโ€™s a step-by-step tutorial for Dockerfile, including explanations and examples for each major command. Dockerfile Tutorial A Dockerfile is a text file containing instructions to build a…

Read More

Docker Tutorials: Docker Image – Example and Sample Programs of Dockerfile

Reference Rajesh Kumar Iโ€™m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I have worked at Cotocus. I share tech blog at DevOps School, travel stories…

Read More

Docker Tutorials: Installation and Configurations

Docker Installation in Centos/RHEL Method -1: How to install Docker Community Edition via YUM? Step 1 – Install required packages. yum-utils provides the yum-config-manager utility, and device-mapper-persistent-data…

Read More

Docker Tutorials: How to Install Docker in Ubuntu?

Install Docker Engine in Ubuntu NOTE – All commands you must run as root user or add a current user into a linux group name called “docker”…

Read More

Docker Lab, Excercise & Assignment – 7 – Docker Volume

Below is a very detailed tutorial and lab manual for learning Docker Volumes, using the Ubuntu image for practical, hands-on labs. This covers all major types of…

Read More

Docker Lab, Excercise & Assignment – 4 – Docker Networking

Hereโ€™s an in-depth, step-by-step tutorial and lab manual for Docker Networkingโ€”starting from basics, covering all core concepts, and providing a hands-on guide to every feature and command….

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