将 docker 与汽车生产线类比, 如果说 docker 引擎是汽车生产车间, 那么容器就是最终的产品 -- 汽车, 而本节要介绍的镜像就如同汽车设计图纸, 其重要性不言而喻 -- 只有有了设计图 (镜像), 才能生产汽车 (汽车).
2 镜像管理
2.1 获取镜像
刚安装好的 docker 里面是没有镜像的, 但我们可以从开源的镜像仓库中下载镜像, docker 官方提供的 docker hub 就是最大的一个 docker 仓库, 里面有全球各地用户提供的各式各样的 docker 镜像供我们下载使用. 在下载镜像之前, 我们先来熟悉一下 docker 镜像下载命令和相关知识.
docker [image] pull 仓库名 [: 标签]
上行命令就是我们常用的 docker 镜像下载命令, 其中, 仓库名指定我们要从哪个仓库下载的镜像, 标签是指镜像便签, 通常指代的是版本信息, 例如: Ubuntu:16.04 和 Ubuntu:18.04 分别指代 Ubuntu 仓库中 16.04 版本的 Ubuntu 和 18.04 版本的 Ubuntu 镜像. 命令中 image 和标签参数可加可不加, 当省略标签参数时, 即指使用默认的 latest 标签, 也就是说, 下载指定仓库中的的最新版本. 下载 Ubuntu 最新版本:
$ docker pull Ubuntu
注意: latest 标签会动态跟随最新版本变更, 这意味着镜像内容也是动态变化的, 所以出于稳定性考虑, 在生产环境中, 不要在生产环境中使用 latest 标签.
pull 命令默认从官方 docker hub 仓库中下载镜像, 所以, 上行命令中并没有指定完整仓库名, 如果需要从其他非官方仓库下载镜像, 则需要指定完整仓库名, 例如, 从网易非常的镜像源中下载 Ubuntu:18.04 镜像:
$ docker pull hub.c.163.com/public/ubuntu18.04
2.2 查看及查找镜像
(1) 查看本机上所有镜像基本信息:
$ docker images
(2) 查看指定镜像详细信息
$ docker [image] inspect 仓库名: 标签
例: 查看上文中下载的 Ubuntu:18.04 镜像详细信息
$ docker inspect Ubuntu:18.04
(3) 查看镜像各层创建信息
$ docker history 仓库名: 标签
(4) 搜索镜像
$ docker search [选项] 关键词
命令中, 可以通过选项指定输出内容格式, 数量, 部分过滤条件等, 关键词是指搜索关键字.
例如: 查找 Ubuntu 相关镜像
$ docker search Ubuntu
2.3 删除镜像
删除镜像可以使用一下两种命令格式:
$ docker rmi 仓库名: 标签或镜像 id
$ docker image rm 仓库名: 标签或镜像 id
定位一个镜像可以通过 "仓库名: 标签" 和镜像 id 两种方式, 所以, 在删除镜像时, 我们也可以通过这两种方式来指定要删除的镜像, 也即是上面两行命令中的仓库名: 标签或镜像 id.
删除镜像时需要注意, 每次运行删除镜像命令事实上只是删除指定镜像的一个标签, 只有当该镜像仅有一个标签时, 使用删除镜像命令才会真正删除镜像. 啥? 一个镜像还有多个标签, 没错, 人类除了真名都还有网名, 小名, 外号呢. 在演示删除镜像前, 我们先尝试给镜像添加标签.
先查看本地有哪些镜像:
- $ docker images
- REPOSITORY TAG IMAGE ID CREATED SIZE
- Ubuntu 18.04 7698f282e524 7 days ago 69.9MB
- hello-world latest fce289e99eb9 4 months ago 1.84kB
- $ docker images
- REPOSITORY TAG IMAGE ID CREATED SIZE
- Ubuntu 18.04 7698f282e524 7 days ago 69.9MB
- bye-world 8000 fce289e99eb9 4 months ago 1.84kB
- hello-world latest fce289e99eb9 4 months ago 1.84kB
- $ docker rmi bye-world:8000
- Untagged: bye-world:8000
- $ docker images
- REPOSITORY TAG IMAGE ID CREATED SIZE
- Ubuntu 18.04 7698f282e524 7 days ago 69.9MB
- hello-world latest fce289e99eb9 4 months ago 1.84kB
- $ docker rmi hello-world:latest
- Error response from daemon: conflict: unable to remove repository reference "hello-world:latest" (must force) - container 53c855a9d4a9 is using its referenced image fce289e99eb9
- $ docker rmi -f hello-world:latest
- Untagged: hello-world:latest
- Untagged: hello-world@sha256:6f744a2005b12a704d2608d8070a494ad1145636eeb74a570c56b94d94ccdbfc
- Deleted: sha256:fce289e99eb9bca977dae136fbe2a82b6b7d4c372474c9235adc1741675f587e
- $ docker images
- REPOSITORY TAG IMAGE ID CREATED SIZE
- Ubuntu 18.04 7698f282e524 7 days ago 69.9MB
- $ docker run -it Ubuntu:18.04 /bin/bash
- root@001b647aad4e:/#
- root@001b647aad4e:/# Git --version
- bash: Git: command not found
- root@001b647aad4e:/# apt-get update
- root@001b647aad4e:/# apt install Git
- root@001b647aad4e:/# Git --version
- Git version 2.17.1
- root@001b647aad4e:/# exit
- exit
- $ docker commit -m "Add git in ubuntu18.04" -a "God" 001b647aad4e ubuntu_git:1.0
- sha256:30e6c7c480dc44743f0f030f4904089fb8568ad2333f8de3a22c4f6a71fdb451
- $ docker images
- REPOSITORY TAG IMAGE ID CREATED SIZE
- ubuntu_git 1.0 30e6c7c480dc About a minute ago 189MB
- Ubuntu 18.04 7698f282e524 8 days ago 69.9MB
- $ mkdir ubuntu_dockerfile_git
- $ cd ubuntu_dockerfile_git/
- FROM Ubuntu:18.04
- RUN apt-get update \
- && apt-get install -y Git \
- && apt-get clean \
- && rm -rf /var/lib/apt/lists/*
- Successfully built 6e703a3b11e2
- Successfully tagged ubuntu_dockerfile_git:1.0
- $ docker images
- $ docker images
- REPOSITORY TAG IMAGE ID CREATED SIZE
- ubuntu_dockerfile_git 1.0 6e703a3b11e2 About a minute ago 163MB
- ubuntu_git 1.0 30e6c7c480dc 20 hours ago 189MB
- Ubuntu 18.04 7698f282e524 9 days ago 69.9MB
- $ docker run -it ubuntu_dockerfile_git:1.0 /bin/bash
- root@18c03ae954ab:/# Git --version
- Git version 2.17.1
来源: https://www.cnblogs.com/chenhuabin/p/10939918.html