使用 docker images 命令列出本地主机已有的镜像。比如:
[root@centos7 ~] docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/busybox latest 22c2dd5ee85d 7 weeks ago 1.16 MB
列表包含了仓库名(REPOSITORY)、标签(TAG)、镜像 ID(IMAGE ID)、创建时间(CREATED)以及所占用的空间(SIZE)。其中镜像ID是进行的唯一标识。
一个仓库有多个tag,可以使用 docker tag 为本地镜像添加新标签。例如为 docker.io/busybox:latest 添加新标签:
docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]
docker tag docker.io/busybox:latest test/busybox:latest
在使用 docker images 查看:
[root@centos7 ~]docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/busybox latest 22c2dd5ee85d 7 weeks ago 1.16 MB
test/busybox latest 22c2dd5ee85d 7 weeks ago 1.16 MB
以上不同标签的镜像ID是完全一致的,说明是指向了同一个镜像文件,只是别名不同,标签起到引用或快捷键的作用。
使用 docker inspect 查看镜像的详细信息。
inspect 命令用于以JSON格式显示容器与镜像的详细信息
docker inspect <选项><容器或镜像名称,id>
若是想查看某一项内容,可以使用 -f 参数指定,如获取镜像的ContainerConfig信息:
docker inspect -f "{{.ContainerConfig.Hostname}}" docker.io/busybox
另:如果从容器信息中获取特定部分,并按照所希望的格式显示:
$ docker run -it -d --name hello -p 8000:80 -p 8080:8080 docker.io/centos /bin/bash
$ docker inspect -f '{{range $p, $conf := .NetworkSettings.Ports}} {{$p}} -> {{(index $conf 0).HostPort}} {{end}}' hello
80/tcp -> 8000 8080/tcp -> 8080
此处使用 {{range $p, $conf := .NetworkSettings.Ports}} 循环访问 .NetworkSettings.Ports 的值,并代入 $p $conf。然后输出$p,并将$conf数组的第一项 (index $conf 0) 的 .HostPort 输出。
另:.NetworkSettings.Ports 是一个map类型数据结构:
map[80/tcp:[{0.0.0.0 8000}] 8080/tcp:[{0.0.0.0 8080}]]