4.1 镜像管理
4.1.1 镜像结构和原理

镜像,即创建容器的模版,含有启动容器所需要的文件系统及所需要的内容,因此镜像主要用于方便和快速的创建并启动容器
镜像含里面是一层层的文件系统,叫做 Union FS(联合文件系统),联合文件系统,可以将几层目录挂载到一起(就像千层饼,洋葱头,俄罗斯套娃一样),形成一个虚拟文件系统,虚拟文件系统的目录结构就像普通 linux 的目录结构一样,镜像通过这些文件再加上宿主机的内核共同提供了一个 linux 的虚拟环境,每一层文件系统叫做一层 layer,联合文件系统可以对每一层文件系统设置三种权限,只读(readonly)、读写(readwrite)和写出(whiteout-able),但是镜像中每一层文件系统都是只读的,构建镜像的时候,从一个最基本的操作系统开始,每个构建提交的操作都相当于做一层的修改,增加了一层文件系统,一层层往上叠加,上层的修改会覆盖底层该位置的可见性,这也很容易理解,就像上层把底层遮住了一样,当使用镜像的时候,我们只会看到一个完全的整体,不知道里面有几层,实际上也不需要知道里面有几层,结构如下:

一个典型的 Linux文件系统由 bootfs 和 rootfs 两部分组成
bootfs(boot file system) 主要包含bootloader和kernel,bootloader主要用于引导加载 kernel,Linux
刚启动时会加载bootfs文件系统,当boot加载完成后,kernel 被加载到内存中后接管系统的控制权,bootfs
会被 umount 掉
rootfs (root file system) 包含的就是典型 Linux 系统中的/dev,/proc,/bin,/etc 等标准目录和文
件,不同的 linux 发行版(如 ubuntu 和 CentOS ) 主要在 rootfs 这一层会有所区别。
一般的镜像通常都比较小,官方提供的Ubuntu镜像只有60MB多点,而 CentOS 基础镜像也只有200MB左右,一些其他版本的镜像甚至只有几MB,比如: busybox 才1.22MB,alpine镜像也只有5M左右。镜像直接调用宿主机的内核,镜像中只提供 rootfs,也就是只需要包括最基本的命令,配置文件和程序库等相关文件就可以了。
4.2 搜索镜像
4.2.1 搜索镜像
4.2.1.1 官网进行镜像搜索

在官方的docker仓库中搜索指定名称的docker镜像,也会有很多第三方镜像,写着offical image的为官方镜像,可信度高。
4.2.1.2 通过命令行进行搜索
命令行格式:
Usage: docker search [OPTIONS] TERM
Search the Docker Hub for images
Options:
-f, --filter filter Filter output based on conditions provided
--format string Pretty-print search using a Go template
--limit int Max number of search results (default 25)
--no-trunc Don't truncate output
范例:
root@ubuntu-2010:~# docker search centos
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
centos The official build of CentOS. 6756 [OK]
ansible/centos7-ansible Ansible on Centos7 134 [OK]
consol/centos-xfce-vnc Centos container with "headless" VNC session… 130 [OK]
jdeathe/centos-ssh OpenSSH / Supervisor / EPEL/IUS/SCL Repos - … 120 [OK]
centos/systemd systemd enabled base container. 101 [OK]
centos/mysql-57-centos7 MySQL 5.7 SQL database server 91
imagine10255/centos6-lnmp-php56 centos6-lnmp-php56 58 [OK]
4.3 常用操作系统基础镜像
4.3.1 alpine介绍

Alpine 操作系统是一个面向安全的轻型的Linux发行版。它不同于通常的Linux发行版,Alpine采用了musl libc 和 busybox 以减小系统的体积和运行时资源消耗,但功能上比busybox又完善很多,因此得到开源社区越来越来的青睐。在保持轻量的同时,还提供了自己的包管理工具apk,可以通过apk命令直接查询和安装各类软件。
Alpine Docker 镜像也继承了 Alpine Linux 发行版的这些优势。相比于其他 Docker 镜像,它的容量非常小,仅仅只有 5 MB 左右(对比 Ubuntu 系列镜像接近 200 MB),且拥有非常友好的包管理机制。官方镜像来自 docker-alpine 项目。
目前 Docker 官方已开始推荐使用 Alpine 替代之前的 Ubuntu 做为基础镜像环境。这样会带来多个好处。包括镜像下载速度加快,镜像安全性提高,主机之间的切换更方便,占用更少磁盘空间等。
- Alpine 官网: https://www.alpinelinux.org/
- Alpine 官方仓库: https://github.com/alpinelinux
- Alpine 官方镜像: https://hub.docker.com/_/alpine/
- Alpine 官方镜像仓库: https://github.com/gliderlabs/docker-alpine
- Alpine 阿里云的镜像仓库: https://mirrors.aliyun.com/alpine/
范例:alpine使用阿里源管理软件
#修改源替换成阿里源,将里面 dl-cdn.alpinelinux.org 的 改成 mirrors.aliyun.com
vi /etc/apk/repositories
http://mirrors.aliyun.com/alpine/v3.8/main/
http://mirrors.aliyun.com/alpine/v3.8/community/
#更新源
apk update
#安装软件
apk add vim
#删除软件
apk del openssh openntp vim
4.3.2 其它基础镜像
- Centos
- Ubuntu
- Debain
- busybox
4.4 下载镜像
4.4.1 下载镜像
从docker仓库将镜像下载到本地,命令格式如下:
Usage: docker pull [OPTIONS] NAME[:TAG|@DIGEST]
Pull an image or a repository from a registry
Options:
-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
镜像下载说明:
root@ubuntu-2010:~# docker pull alpine
Using default tag: latest #默认下载最新版本
latest: Pulling from library/alpine
a0d0a0d46f8b: Pull complete #分层下载
Digest: sha256:e1c082e3d3c45cccac829840a25941e679c25d438cc8412c2fa221cf1a824e6a
Status: Downloaded newer image for alpine:latest
docker.io/library/alpine:latest #下载的完整地址
镜像下载保存的路径
/var/lib/docker/overlay2/镜像ID
注意:镜像下载完成后,会自动解压缩,比官网显示的可能会大很多
4.4.2 docker镜像加速配置
- 可在阿里云申请镜像加速器
- 网易云的镜像加速器: http://hub-mirror.c.163.com/
- 腾讯云的镜像加速器:https://mirror.ccs.tencentyun.com
配置范例:
1. 安装/升级Docker客户端
推荐安装1.10.0以上版本的Docker客户端,参考文档 docker-ce
2. 配置镜像加速器
修改daemon配置文件/etc/docker/daemon.json来使用加速器
mkdir -p /etc/docker
vim /etc/docker/daemon.json
{
"registry-mirrors": ["http://hub-mirror.c.163.com/"]
}
systemctl daemon-reload
systemctl restart docker
4.5 查看本地镜像
docker images 可以查看下载至本地的镜像
格式:
Usage: docker images [OPTIONS] [REPOSITORY[:TAG]]
List images
Options:
-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 image IDs
范例:
root@ubuntu-2010:~# docker images --digests
REPOSITORY TAG DIGEST IMAGE ID CREATED SIZE
alpine latest sha256:e1c082e3d3c45cccac829840a25941e679c25d438cc8412c2fa221cf1a824e6a 14119a10abf4 2 weeks ago 5.6MB
root@ubuntu-2010:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
alpine latest 14119a10abf4 2 weeks ago 5.6MB
root@ubuntu-2010:~# docker images --no-trunc
REPOSITORY TAG IMAGE ID CREATED SIZE
alpine latest sha256:14119a10abf4669e8cdbdff324a9f9605d99697215a0d21c360fe8dfa8471bab 2 weeks ago 5.6MB
root@ubuntu-2010:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
alpine latest 14119a10abf4 2 weeks ago 5.6MB
root@ubuntu-2010:~# docker images -q
14119a10abf4
#查看指定镜像的信息
root@ubuntu-2010:~# docker image inspect alpine
[
{
"Id": "sha256:14119a10abf4669e8cdbdff324a9f9605d99697215a0d21c360fe8dfa8471bab",
"RepoTags": [
"alpine:latest"
],
"RepoDigests": [
"alpine@sha256:e1c082e3d3c45cccac829840a25941e679c25d438cc8412c2fa221cf1a824e6a"
],
"Parent": "",
"Comment": "",
"Created": "2021-08-27T17:19:45.758611523Z",
"Container": "330289c649db86f5fb1ae5bfef18501012b550adb0638b9193d4a3a4b65a2f9b",
"ContainerConfig": {
"Hostname": "330289c649db",
"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/sh\"]"
],
"Image": "sha256:d3e0b6258ec2f725c19668f11ae5323c3b0245e197ec478424ec6a87935690eb",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {}
},
"DockerVersion": "20.10.7",
"Author": "",
"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/sh"
],
"Image": "sha256:d3e0b6258ec2f725c19668f11ae5323c3b0245e197ec478424ec6a87935690eb",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": null
},
"Architecture": "amd64",
"Os": "linux",
"Size": 5595292,
"VirtualSize": 5595292,
"GraphDriver": {
"Data": {
"MergedDir": "/var/lib/docker/overlay2/32609ab91d575aa9ed94437e9800c87cc8bde73342b5ad2f7dbaf6c7399b584f/merged",
"UpperDir": "/var/lib/docker/overlay2/32609ab91d575aa9ed94437e9800c87cc8bde73342b5ad2f7dbaf6c7399b584f/diff",
"WorkDir": "/var/lib/docker/overlay2/32609ab91d575aa9ed94437e9800c87cc8bde73342b5ad2f7dbaf6c7399b584f/work"
},
"Name": "overlay2"
},
"RootFS": {
"Type": "layers",
"Layers": [
"sha256:e2eb06d8af8218cfec8210147357a68b7e13f7c485b991c288c2d01dc228bb68"
]
},
"Metadata": {
"LastTagTime": "0001-01-01T00:00:00Z"
}
}
]
4.6 镜像导出和导入
4.6.1 镜像导出
利用docker save 命令可以将本地镜像导出为一个打包tar文件,然后复制到其他服务器进行导入使用
格式:
docker save [OPTIONS] IMAGE [IMAGE...]
选项:
-o, --output string Write to a file, instead of STDOUT
#说明:
Docker save 使用IMAGE ID导出,在导入后的镜像没有REPOSITORY和TAG,显示为<none>
常见用法:
docker save IMAGE -o /path/file.tar
docker save IMAGE > /path/file.tar
范例:
root@ubuntu-2010:~# docker save alpine:latest -o alpine-1.tar
root@ubuntu-2010:~# docker save alpine:latest > alpine-2.tar
root@ubuntu-2010:~# ll alpine*
-rw------- 1 root root 5874688 Sep 16 11:09 alpine-1.tar
-rw-r--r-- 1 root root 5874688 Sep 16 11:09 alpine-2.tar
4.6.2 镜像导入
利用docker load命令可以将镜像导出的压缩文件再导入
格式:
docker load [OPTIONS]
#选项
-i, --input string Read from tar archive file, instead of STDIN
-q, --quiet Suppress the load output
常见用法:
docker load -i /path/file.tar
docker load < /path/file.tar
范例:
root@ubuntu-2010:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
root@ubuntu-2010:~# docker load -i mysql.tar
d000633a5681: Loading layer [==================================================>] 72.53MB/72.53MB
731f7c213603: Loading layer [==================================================>] 338.4kB/338.4kB
b2830993d63a: Loading layer [==================================================>] 9.557MB/9.557MB
97a2a3481f0d: Loading layer [==================================================>] 4.202MB/4.202MB
35e94dafc854: Loading layer [==================================================>] 2.048kB/2.048kB
2eaac5532d44: Loading layer [==================================================>] 53.77MB/53.77MB
89638afc97cd: Loading layer [==================================================>] 5.632kB/5.632kB
cf0cecb7358f: Loading layer [==================================================>] 3.584kB/3.584kB
16c71bbd693d: Loading layer [==================================================>] 378.8MB/378.8MB
46bb27bfe165: Loading layer [==================================================>] 5.632kB/5.632kB
2ecd2889304a: Loading layer [==================================================>] 17.92kB/17.92kB
bc3da4909d75: Loading layer [==================================================>] 1.536kB/1.536kB
Loaded image: mysql:latest
root@ubuntu-2010:~# docker load < alpine-1.tar
e2eb06d8af82: Loading layer [==================================================>] 5.865MB/5.865MB
Loaded image: alpine:latest
root@ubuntu-2010:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql latest 0716d6ebcc1a 13 days ago 514MB
alpine latest 14119a10abf4 2 weeks ago 5.6MB
4.7 删除镜像
docker rmi 命令可以删除本地镜像
格式:
docker rmi [OPTIONS] IMAGE [IMAGE...]
#选项:
-f, --force Force removal of the image
--no-prune Do not delete untagged parents
范例:
root@ubuntu-2010:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql latest 0716d6ebcc1a 13 days ago 514MB
alpine latest 14119a10abf4 2 weeks ago 5.6MB
root@ubuntu-2010:~# docker rmi mysql 14119a10abf4
Untagged: mysql:latest
Deleted: sha256:0716d6ebcc1a61c5a296fcb187e71f93531e510d4e4400267e2e502103d0194c
Deleted: sha256:dc895a08d34b5b81fc4ca087d2ad52cbe1a2050e249040a22c5f2eabf2f384ba
Deleted: sha256:660229dcf1a452460127a498b9f3f161e7ca94507353ded8af92fe9ab55a32ed
Deleted: sha256:6b26fa2fc4e2150aee2f2557bcbfaf727c00d1650ea08d8ed3fe7c8a6caaa88b
Deleted: sha256:c20303553d5d2594e1655000089b12eca8db7afdcb068cc35fc47ebfe3dab5fb
Deleted: sha256:77a3d69619bfea7b30831a41a32bbf61756c9f95513743deea8daa9a83ff2646
Deleted: sha256:9578f1c7f00f400b3f71be0ee721cbc0892e05e454323e1a74a6e56ae1dafdab
Deleted: sha256:335f9f9fbbd8977530806ed5439a2b67f1c06117f752a2598698de4ae304c516
Deleted: sha256:e15ed274d47a7d6ddff0afcc628143254c69128a9d2379900ebb519e7c6c2bce
Deleted: sha256:51930b767631b583738d75519bed2a8cc757c5b0c904617972386462deee2ca7
Deleted: sha256:43bd682fb659113a8ab168032d8f82dee86d2cee5cee2e146af6c3a6f9ccef18
Deleted: sha256:1957f1873568b423369e0299de6c9b75a111fea807b0c07506ba45d075ca8f80
Deleted: sha256:d000633a56813933cb0ac5ee3246cf7a4c0205db6290018a169d7cb096581046
Untagged: alpine:latest
Deleted: sha256:14119a10abf4669e8cdbdff324a9f9605d99697215a0d21c360fe8dfa8471bab
Deleted: sha256:e2eb06d8af8218cfec8210147357a68b7e13f7c485b991c288c2d01dc228bb68
root@ubuntu-2010:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
4.8 镜像打标签
dacker tag 可以给镜像打标签,类似于起别名,用于记录版本和用途
格式:
docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]
TAG默认为latest
范例:
root@ubuntu-2010:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
alpine latest 14119a10abf4 2 weeks ago 5.6MB
root@ubuntu-2010:~# docker tag alpine:latest alpine:v1.0
root@ubuntu-2010:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
alpine latest 14119a10abf4 2 weeks ago 5.6MB
alpine v1.0 14119a10abf4 2 weeks ago 5.6MB
4.9 总结
- 常见操作:搜索、下载、导出、导入、删除、推送(push,将在私有仓库时候讲述)
- 命令总结
docker search images
docker pull images
docker save -o images images.tar
docker load -i images.tar
docker rmi images [ID]