Docker history command
docker historyThe command is used to view the historical layer information of a specified image, showing each layer during the image creation process, including creation time, creator, size, comments, and other information.
Syntax
docker history [OPTIONS] IMAGE
IMAGE: The image name or ID whose history is to be viewed.
OPTIONS description:
-H, --human: Displays the image size in a human-readable format (enabled by default).--no-trunc: Displays the full output without truncating information.-q, --quietOnly display the image ID.
Example
1. View image history
docker history myimage:latest
Output example:
IMAGE CREATED CREATED BY SIZE COMMENT sha256:123abc456def 2 days ago /bin/sh -c #(nop) CMD ["nginx" "-g" "daemon… 0B sha256:789ghi012jkl 2 days ago /bin/sh -c #(nop) COPY file:abc123 in /var/… 1.5kB sha256:345mno678pqr 2 days ago /bin/sh -c apt-get update && apt-get install… 45.3MB sha256:678stu901vwx 2 days ago /bin/sh -c #(nop) LABEL maintainer=yourname… 0B sha256:901yza234bcd 2 days ago /bin/sh -c #(nop) FROM ubuntu:20.04 72.9MB
2. Display full output
docker history --no-trunc myimage:latest
3. Only display the image ID
docker history -q myimage:latest
Output example:
sha256:123abc456def sha256:789ghi012jkl sha256:345mno678pqr sha256:678stu901vwx sha256:901yza234bcd
Operation examples
Build a simple image
1. Create a Dockerfile:
# 使用 Ubuntu 作为基础镜像 FROM ubuntu:20.04 # 添加维护者信息 LABEL maintainer="yourname@example.com" # 更新包列表并安装 Nginx RUN apt-get update && apt-get install -y nginx # 复制自定义网页到 Nginx 的默认网页目录 COPY index.html /var/www/html/ # 设置启动时的默认命令 CMD ["nginx", "-g", "daemon off;"]
2. Build the image:
docker build -t mynginx:latest .
3. View image history
docker history mynginx:latest
Output example:
IMAGE CREATED CREATED BY SIZE COMMENT sha256:123abc456def 1 minute ago /bin/sh -c #(nop) CMD ["nginx" "-g" "daemon… 0B sha256:789ghi012jkl 1 minute ago /bin/sh -c #(nop) COPY file:abc123 in /var/… 1.5kB sha256:345mno678pqr 1 minute ago /bin/sh -c apt-get update && apt-get install… 45.3MB sha256:678stu901vwx 1 minute ago /bin/sh -c #(nop) LABEL maintainer=yourname… 0B sha256:901yza234bcd 1 minute ago /bin/sh -c #(nop) FROM ubuntu:20.04
Notes
- Image history information includes the creation command and size of each layer, helping to understand the build process and contents of the image.
- Usage
--no-truncThe option allows viewing the complete creation command, preventing information truncation. - When building complex images, viewing the history can help identify and optimize redundant steps in the Dockerfile.
docker historyThe command is a powerful tool that can help developers and operations personnel understand the build history of an image and detailed information about each layer. By viewing the image history, you can better debug, optimize, and audit Docker images, ensuring the efficiency and security of the images.
other extensions