Docker - Docker Image及Image命令详解

什么是镜像(Image)

Docker镜像(Docker Image)就是一个只读的模板。比如,一个镜像可以包含一个完整的Ubuntu操作系统环境。镜像可以用来创建Docker容器。

在Docker的术语里,一个只读层被称为镜像,一个镜像是永远不会变的。

由于Docker使用一个统一文件系统,Docker进程认为整个文件系统是以读写方式挂载的。 但是所有的变更都发生在顶层的可写层,而下层的原始的只读镜像文件并未变化。由于镜像不可写,所以镜像是无状态的。

父镜像

每一个镜像都可能依赖于由一个或多个下层镜像组成的另一个镜像。我们有时说,下层那个镜像是上层镜像的父镜像。而没有任何父镜像的镜像,谓之基础镜像(Base Image)。

可以看一下这篇博客,会让你对Docker的整体架构有一个很清晰的了解:Docker - 这应该就是你想要的Docker架构分析。

Docker Image Help

通过该命令可以查看Image下有哪些命令。

docker image help
[root@izoq008ryseuupz ~]# docker image helpUsage:   docker image COMMANDManage imagesCommands:build       Build an image from a Dockerfilehistory     Show the history of an imageimport      Import the contents from a tarball to create a filesystem imageinspect     Display detailed information on one or more imagesload        Load an image from a tar archive or STDINls          List imagesprune       Remove unused imagespull        Pull an image or a repository from a registrypush        Push an image or a repository to a registryrm          Remove one or more imagessave        Save one or more images to a tar archive (streamed to STDOUT by default)tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGERun 'docker image COMMAND --help' for more information on a command.

通过docker image COMMAND --help来知道每一个Image命令的详细信息。

Run 'docker image COMMAND --help' for more information on a command.

Docker Image LS

docker image ls --help
[root@izoq008ryseuupz ~]# docker image ls --helpUsage:  docker image ls [OPTIONS] [REPOSITORY[:TAG]]List imagesAliases:ls, images, listOptions:-a, --all             Show all images (default hides intermediate images)--digests         Show digests-f, --filter filter   Filter output based on conditions provided--format string   Pretty-print images using a Go template--no-trunc        Don't truncate output-q, --quiet           Only show numeric IDs

比如docker image ls -a,展示所有Image的关键信息。

[root@izoq008ryseuupz ~]# docker image ls -a
REPOSITORY                                                     TAG                 IMAGE ID            CREATED             SIZE
node                                                           latest              5377c9a2fb1f        5 weeks ago         943MB
openzipkin/zipkin                                              latest              1850194f377c        3 months ago        160MB
rancher/server                                                 stable              98d8bb571885        6 months ago        1.08GB
redis                                                          5.0.7               7eed8df88d3b        8 months ago        98.2MB
hello-world                                                    latest              bf756fb1ae65        10 months ago       13.3kB

docker imagesdocker image lsdocker image ls -a是一样的效果。

比如docker image ls -q,就只输出Image的IMAGE ID 。

[root@izoq008ryseuupz ~]# docker image ls -q
5377c9a2fb1f
1850194f377c
98d8bb571885
7eed8df88d3b
bf756fb1ae65

其他的命令选项可以自己试一试。

Docker Image Pull

拉取镜像。

docker image pull --help
[root@izoq008ryseuupz ~]# docker image pull --helpUsage:    docker image pull [OPTIONS] NAME[:TAG|@DIGEST]Pull an image or a repository from a registryOptions:-a, --all-tags                Download all tagged images in the repository--disable-content-trust   Skip image verification (default true)--platform string         Set platform if server is multi-platform capable-q, --quiet                   Suppress verbose output

拉取centos:7镜像。

docker image pull centos:7
[root@izoq008ryseuupz ~]# docker image pull centos:7
7: Pulling from library/centos
2d473b07cdd5: Pull complete
Digest: sha256:0f4ec88e21daf75124b8a9e5ca03c37a5e937e0e108a255d890492430789b60e
Status: Downloaded newer image for centos:7
docker.io/library/centos:7

拉取centos:6镜像,只想要输出pull的简要信息,不输出pull的详细信息,如Downloading的进度。

docker image pull -q centos:6
[root@izoq008ryseuupz ~]# docker image pull -q centos:6
docker.io/library/centos:6

其他的命令选项可以自己试一试。

Docker Image Prune

此命令会把所有未使用的镜像进行删除(Remove unused images),慎用!

docker image prune --help
[root@izoq008ryseuupz ~]# docker image prune --helpUsage:   docker image prune [OPTIONS]Remove unused imagesOptions:-a, --all             Remove all unused images, not just dangling ones--filter filter   Provide filter values (e.g. 'until=<timestamp>')-f, --force           Do not prompt for confirmation
docker image prune
[root@izoq008ryseuupz ~]# docker image prune
WARNING! This will remove all dangling images.
Are you sure you want to continue? [y/N] y
Total reclaimed space: 0B

Docker Image RM

删除指定镜像。

docker image rm --help
[root@izoq008ryseuupz ~]# docker image rm --helpUsage:  docker image rm [OPTIONS] IMAGE [IMAGE...]Remove one or more imagesAliases:rm, rmi, removeOptions:-f, --force      Force removal of the image--no-prune   Do not delete untagged parents

删除centos:7

docker image rm centos:7
[root@izoq008ryseuupz ~]# docker image rm centos:7
Untagged: centos:7
Untagged: centos@sha256:0f4ec88e21daf75124b8a9e5ca03c37a5e937e0e108a255d890492430789b60e
Deleted: sha256:8652b9f0cb4c0599575e5a003f5906876e10c1ceb2ab9fe1786712dac14a50cf
Deleted: sha256:174f5685490326fc0a1c0f5570b8663732189b327007e47ff13d2ca59673db02

强制删除centos:6

docker image rm -f centos:6
[root@izoq008ryseuupz ~]# docker image rm -f centos:6
Untagged: centos:6
Untagged: centos@sha256:dec8f471302de43f4cfcf82f56d99a5227b5ea1aa6d02fa56344986e1f4610e7
Deleted: sha256:d0957ffdf8a2ea8c8925903862b65a1b6850dbb019f88d45e927d3d5a3fa0c31
Deleted: sha256:af6bf1987c2eb07d73f33836b0d8fd825d7c785273526b077e46780e8b4b2ae9

其他的命令选项可以自己试一试。

Docker Image Tag

对原有镜像打tag,会生成新镜像。

docker image tag --help
[root@izoq008ryseuupz ~]# docker image tag --helpUsage: docker image tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
docker image tag centos:6 centos:kaven.blog

Docker Image Save

保存镜像。

docker image save --help
[root@izoq008ryseuupz ~]# docker image save --helpUsage:    docker image save [OPTIONS] IMAGE [IMAGE...]Save one or more images to a tar archive (streamed to STDOUT by default)Options:-o, --output string   Write to a file, instead of STDOUT
docker image save centos:kaven.blog > kaven.blog.tar
[root@izoq008ryseuupz ~]# docker image save centos:kaven.blog > kaven.blog.tar   #保存到当前目录
[root@izoq008ryseuupz ~]# ls kaven*
kaven.blog.tar
docker image save centos:kaven.blog -o /usr/kaven.blog.tar
[root@izoq008ryseuupz ~]# docker image save centos:kaven.blog -o /usr/kaven.blog.tar #保存到别的目录
[root@izoq008ryseuupz ~]# ls /usr/kaven*
/usr/kaven.blog.tar

Docker Image Load

加载镜像。

docker image load --help
[root@izoq008ryseuupz ~]# docker image load --helpUsage:    docker image load [OPTIONS]Load an image from a tar archive or STDINOptions:-i, --input string   Read from tar archive file, instead of STDIN-q, --quiet          Suppress the load output
docker image load < kaven.blog.tar
[root@izoq008ryseuupz ~]# docker image rm centos:kaven.blog
Untagged: centos:kaven.blog
[root@izoq008ryseuupz ~]# docker image load < kaven.blog.tar
Loaded image: centos:kaven.blog

docker image load -i /usr/kaven.blog.tar
[root@izoq008ryseuupz ~]# docker image rm centos:kaven.blog
Untagged: centos:kaven.blog
[root@izoq008ryseuupz ~]# docker image load -i /usr/kaven.blog.tar
Loaded image: centos:kaven.blog


其他的命令选项可以自己试一试。

Docker Image History

显示镜像的操作历史。

docker image history  --help
[root@izoq008ryseuupz ~]# docker image history  --helpUsage:    docker image history [OPTIONS] IMAGEShow the history of an imageOptions:--format string   Pretty-print images using a Go template-H, --human           Print sizes and dates in human readable format (default true)--no-trunc        Don't truncate output-q, --quiet           Only show numeric IDs
docker image history centos:kaven.blog
[root@izoq008ryseuupz ~]# docker image history centos:kaven.blog
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
d0957ffdf8a2        20 months ago       /bin/sh -c #(nop)  CMD ["/bin/bash"]            0B
<missing>           20 months ago       /bin/sh -c #(nop)  LABEL org.label-schema.sc…   0B
<missing>           20 months ago       /bin/sh -c #(nop) ADD file:0065316a41144e95b…   194MB
<missing>           2 years ago         /bin/sh -c #(nop)  MAINTAINER https://github…   0B

只想展示镜像操作历史的简要信息(IMAGE)。

docker image history -q centos:6
[root@izoq008ryseuupz ~]# docker image history -q centos:6
d0957ffdf8a2
<missing>
<missing>
<missing>

其他的命令选项可以自己试一试。

Docker Image Inspect

显示镜像的详细信息。

docker image inspect --help
[root@izoq008ryseuupz ~]# docker image inspect --helpUsage: docker image inspect [OPTIONS] IMAGE [IMAGE...]Display detailed information on one or more imagesOptions:-f, --format string   Format the output using the given Go template
docker image inspect centos:6
[root@izoq008ryseuupz ~]# docker image inspect centos:6
[{"Id": "sha256:d0957ffdf8a2ea8c8925903862b65a1b6850dbb019f88d45e927d3d5a3fa0c31","RepoTags": ["centos:6","centos:kaven.blog"],"RepoDigests": ["centos@sha256:dec8f471302de43f4cfcf82f56d99a5227b5ea1aa6d02fa56344986e1f4610e7"],"Parent": "","Comment": "","Created": "2019-03-14T21:20:11.486358099Z","Container": "d519f3e5c41d16388d3fba0dac626427b21deb98cce150dee80c180b9baf9435","ContainerConfig": {"Hostname": "d519f3e5c41d","Domainname": "","User": "","AttachStdin": false,"AttachStdout": false,"AttachStderr": false,"Tty": false,"OpenStdin": false,"StdinOnce": false,"Env": ["PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"],"Cmd": ["/bin/sh","-c","#(nop) ","CMD [\"/bin/bash\"]"],"ArgsEscaped": true,"Image": "sha256:143abcd43bce45f4fd9ba51c7361051d7ea9e9e1eadb66e5c94a9c1b7754524f","Volumes": null,"WorkingDir": "","Entrypoint": null,"OnBuild": null,"Labels": {"org.label-schema.build-date": "20181006","org.label-schema.license": "GPLv2","org.label-schema.name": "CentOS Base Image","org.label-schema.schema-version": "1.0","org.label-schema.vendor": "CentOS"}},"DockerVersion": "18.06.1-ce","Author": "https://github.com/CentOS/sig-cloud-instance-images","Config": {"Hostname": "","Domainname": "","User": "","AttachStdin": false,"AttachStdout": false,"AttachStderr": false,"Tty": false,"OpenStdin": false,"StdinOnce": false,"Env": ["PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"],"Cmd": ["/bin/bash"],"ArgsEscaped": true,"Image": "sha256:143abcd43bce45f4fd9ba51c7361051d7ea9e9e1eadb66e5c94a9c1b7754524f","Volumes": null,"WorkingDir": "","Entrypoint": null,"OnBuild": null,"Labels": {"org.label-schema.build-date": "20181006","org.label-schema.license": "GPLv2","org.label-schema.name": "CentOS Base Image","org.label-schema.schema-version": "1.0","org.label-schema.vendor": "CentOS"}},"Architecture": "amd64","Os": "linux","Size": 193901906,"VirtualSize": 193901906,"GraphDriver": {"Data": {"MergedDir": "/var/lib/docker/overlay2/ae0f8ac6c397e63561b52142c4e945909b8af2351d28e5af66f3490ff7078587/merged","UpperDir": "/var/lib/docker/overlay2/ae0f8ac6c397e63561b52142c4e945909b8af2351d28e5af66f3490ff7078587/diff","WorkDir": "/var/lib/docker/overlay2/ae0f8ac6c397e63561b52142c4e945909b8af2351d28e5af66f3490ff7078587/work"},"Name": "overlay2"},"RootFS": {"Type": "layers","Layers": ["sha256:af6bf1987c2eb07d73f33836b0d8fd825d7c785273526b077e46780e8b4b2ae9"]},"Metadata": {"LastTagTime": "2020-11-22T11:46:01.910880961+08:00"}}
]

其他的命令选项可以自己试一试。

Docker Image Import

从归档文件中创建镜像。

docker image import --help
[root@izoq008ryseuupz ~]# docker image import --helpUsage:  docker image import [OPTIONS] file|URL|- [REPOSITORY[:TAG]]Import the contents from a tarball to create a filesystem imageOptions:-c, --change list       Apply Dockerfile instruction to the created image-m, --message string    Set commit message for imported image--platform string   Set platform if server is multi-platform capable
docker image import  kaven.blog.tar centos:6.kaven
[root@izoq008ryseuupz ~]# docker image import  kaven.blog.tar centos:6.kaven
sha256:3a568400d73096f71c5f5165f110414bb83e8024bca2ee3eea77336a194e920f


其他的命令选项可以自己试一试。

Docker Image Push

上传镜像到仓库。

docker image push --help
[root@izoq008ryseuupz ~]# docker image push --helpUsage:    docker image push [OPTIONS] NAME[:TAG]Push an image or a repository to a registryOptions:--disable-content-trust   Skip image signing (default true)

Docker Image Build

创建镜像。

docker image build --help
[root@izoq008ryseuupz ~]# docker image build --helpUsage:   docker image build [OPTIONS] PATH | URL | -Build an image from a DockerfileOptions:--add-host list           Add a custom host-to-IP mapping (host:ip)--build-arg list          Set build-time variables--cache-from strings      Images to consider as cache sources--cgroup-parent string    Optional parent cgroup for the container--compress                Compress the build context using gzip--cpu-period int          Limit the CPU CFS (Completely Fair Scheduler) period--cpu-quota int           Limit the CPU CFS (Completely Fair Scheduler) quota-c, --cpu-shares int          CPU shares (relative weight)--cpuset-cpus string      CPUs in which to allow execution (0-3, 0,1)--cpuset-mems string      MEMs in which to allow execution (0-3, 0,1)--disable-content-trust   Skip image verification (default true)-f, --file string             Name of the Dockerfile (Default is 'PATH/Dockerfile')--force-rm                Always remove intermediate containers--iidfile string          Write the image ID to the file--isolation string        Container isolation technology--label list              Set metadata for an image-m, --memory bytes            Memory limit--memory-swap bytes       Swap limit equal to memory plus swap: '-1' to enable unlimited swap--network string          Set the networking mode for the RUN instructions during build (default "default")--no-cache                Do not use cache when building the image-o, --output stringArray      Output destination (format: type=local,dest=path)--platform string         Set platform if server is multi-platform capable--progress string         Set type of progress output (auto, plain, tty). Use plain to show container output (default "auto")--pull                    Always attempt to pull a newer version of the image-q, --quiet                   Suppress the build output and print image ID on success--rm                      Remove intermediate containers after a successful build (default true)--secret stringArray      Secret file to expose to the build (only if BuildKit enabled): id=mysecret,src=/local/secret--security-opt strings    Security options--shm-size bytes          Size of /dev/shm--squash                  Squash newly built layers into a single new layer--ssh stringArray         SSH agent socket or keys to expose to the build (only if BuildKit enabled) (format: default|<id>[=<socket>|<key>[,<key>]])--stream                  Stream attaches to server to negotiate build context-t, --tag list                Name and optionally a tag in the 'name:tag' format--target string           Set the target build stage to build.--ulimit ulimit           Ulimit options (default [])

这里会介绍将一个简单的C语言程序,Build成一个Image。

先在当前目录下创建一个hello.c程序。

vim hello.c

程序如下:

#include<stdio.h>int main()
{printf("hello kaven\n");printf("this is docker\n");
}

不知道怎么退出VIM,可以看一下这篇博客:怎么保存退出 vim 编辑。

[root@izoq008ryseuupz ~]# ls
hello.c  kaven.blog.tar  logs  mall.jar
[root@izoq008ryseuupz ~]# gcc hello.c -o hello
[root@izoq008ryseuupz ~]# ls
hello  hello.c  kaven.blog.tar  logs  mall.jar
[root@izoq008ryseuupz ~]# ./hello
hello kaven
this is docker

在当前目录下创建Dockerfile

vim Dockerfile

输入:

FROM scratch
ADD hello /
CMD ["/hello"]

现在看不懂没关系,自己跟着体验一次,之后的博客会详细讲解这些Dockerfile操作。

docker image build -t kaven/hello:v1 .
[root@izoq008ryseuupz ~]# docker image build -t kaven/hello:v1 .
Sending build context to Docker daemon  564.4MB
Step 1/3 : FROM scratch--->
Step 2/3 : ADD hello /---> 5d49f50c725b
Step 3/3 : CMD ["/hello"]---> Running in b0b3bdce9078
Removing intermediate container b0b3bdce9078---> b1013ca7925d
Successfully built b1013ca7925d
Successfully tagged kaven/hello:v1

这样kaven/hello:v1就有了。

Docker Image及Image命令详解就介绍到这里。

写博客是博主记录自己的学习过程,如果有错误,请指正,谢谢!

Docker - Docker Image及Image命令详解相关推荐

  1. docker exec执行多个命令详解

    2019独角兽企业重金招聘Python工程师标准>>> docker exec执行多个命令详解 2018年04月23日 22:46:24 阅读数:6928 标签: dockercon ...

  2. linux的备份命令详解,docker备份linux系统的命令详解

    tar备份系统 sudo tar cvpzf backup.tgz --exclude=/proc --exclude=/mnt --exclude=/sys --exclude=/backup.tg ...

  3. docker常用命令详解

    docker常用命令详解 本文只记录docker命令在大部分情境下的使用,如果想了解每一个选项的细节,请参考官方文档,这里只作为自己以后的备忘记录下来. 根据自己的理解,总的来说分为以下几种: Doc ...

  4. docker ps命令详解 列出运行中的容器

    docker ps命令详解 列出运行中的容器 使用docker ps命令即可列出运行中的容器,执行该命令后,会出现如下7列表格 CONTAINER_ID      表示容器ID IMAGE       ...

  5. docker build命令详解_Docker镜像与容器常用命令图文详解

    一.系统命令 #查看docker版本 docker version #查看docker的系统信息,包含镜像容器的数量 docker info #查看docker的所有命令 docker --help ...

  6. Docker - Docker Container及Container命令详解

    Docker - Docker Container及Container命令详解 什么是Docker容器(Docker Container) 上一篇博客介绍了Docker镜像(Docker Image) ...

  7. Docker - Docker Volume及Volume命令详解

    Docker - Docker Volume及Volume命令详解 什么是数据卷(Volume) Docker镜像被存储在一系列的只读层中.当我们创建一个容器时,Docker会读取镜像(只读),并在其 ...

  8. docker images 命令详解

    [原文链接]docker images 命令详解 文章目录 一.docker images 命令选项 二.docker images 使用实例 2.1 显示最近创建的镜像 2.2 根据镜像名和tag值 ...

  9. docker inspect命令详解

    docker inspect命令详解 1.作用 获取容器/镜像的元数据(JSON格式) 2.语法格式 docker inspect [OPTIONS] NAME|ID [NAME|ID...] -f ...

  10. 26-Docker-常用命令详解-docker rmi

    常用命令详解-docker rmi 前言 docker rmi 语法格式 options说明 使用示例 删除镜像,且不带 tag 删除镜像,且带 tag 根据镜像ID,强制删除镜像 删除多个镜像 删除 ...

最新文章

  1. 看漫画学Python,屏幕前的彦祖要不要试试?
  2. IOS:批量导入图片和视频到模拟器的相册
  3. 配置HTTPS以与Servlet一起使用
  4. python 数据分析 实际案例_python实战案例:超市营业额数据分析
  5. 笔记一 Redis基础
  6. oracle 9i故障恢复,Oracle9i rman备份恢复总结
  7. KVM命令行使用工具(virsh)详解。
  8. the JDBC Driver has been forcibly unregistered.
  9. 正则表达式匹配身份证号
  10. selenium鼠标操作
  11. V4L2视频输入框架概述
  12. 推荐四个不错的公众号
  13. 计算机处理数据几进制,计算机内部运行和处理的数据是几进制
  14. 驱动开发指南 第八章 汇编LED灯实验
  15. Linux下的一些时间说明
  16. SJA1000验收滤波器使用
  17. 从qq空间看Tencent
  18. linux设备驱动归纳总结(八):1.总线、设备和驱动
  19. 踔厉奋发,笃行不怠——2022年度引迈信息年终总结
  20. 计算机程序手工编织,手工编程

热门文章

  1. 终身受用的十大经典理论
  2. html 外联 变 内联,Html 内联元素、外联元素 和 可变元素
  3. 语法长难句-----名词和名词性从句
  4. css 一直图片适配所有手机背景图
  5. 在线模拟linux终端,linux下tty,控制台,虚拟终端,串口,console(控制台终端)详解...
  6. 2007年“网络十大炒女”排行榜
  7. 文化财经SAR指标计算(二)
  8. mysql 按照年龄段分组查询
  9. 天涯上令人肝肠寸断的100个签名
  10. 50个查询系列-第五个查询:查询没学过“叶平”老师课的同学的学号、姓名;...