8. 查看镜像历史

docker history 命令用于查看 Docker 镜像的创建历史,显示该镜像的每一层(Layer)的详细信息。

这个命令能够提供如下信息:

通过该命令分析镜像的组成,可以排查层体积过大问题或验证创建过程是否符合预期要求。

命令格式

docker history 镜像ID
# 或
docker history 镜像名称[:标签名称]

示例

weimingze@mzstudio:~$ sudo docker history ubuntu:24.04
IMAGE          CREATED        CREATED BY                                      SIZE      COMMENT
602eb6fb314b   2 months ago   /bin/sh -c #(nop)  CMD ["/bin/bash"]            0B
<missing>      2 months ago   /bin/sh -c #(nop) ADD file:1d7c45546e94b90e9…   78.1MB
<missing>      2 months ago   /bin/sh -c #(nop)  LABEL org.opencontainers.…   0B
<missing>      2 months ago   /bin/sh -c #(nop)  LABEL org.opencontainers.…   0B
<missing>      2 months ago   /bin/sh -c #(nop)  ARG LAUNCHPAD_BUILD_ARCH     0B
<missing>      2 months ago   /bin/sh -c #(nop)  ARG RELEASE                  0B

信息显示 ubuntu:24.04 这个镜像一共有 6 层(每次修改会多出一层),最后一层是 602eb6fb314b,并将启动命令改为 /bin/bash