docker镜像管理基础
镜像的概念
镜像可以理解为应用程序的集装箱,而docker用来装卸集装箱。
docker镜像含有启动容器所需要的文件系统及其内容,因此,其用于创建并启动容器。
docker镜像采用分层构建机制,最底层为bootfs,其上为rootfs
- bootfs:用于系统引导的文件系统,包括bootloader和kernel,容器启动完成后会被卸载以节约内存资源
- rootfs:位于bootfs之上,表现为docker容器的根文件系统
- 传统模式中,系统启动之时,内核挂载rootfs会首先将其挂载为“只读”模式,完整性自检完成后将其重新挂载为读写模式
- docker中,rootfs由内核挂载为“只读”模式,而后通过“联合挂载”技术额外挂载一个“可写”层
注意:当删除容器时,这个容器自有的“可写”层会一起被删除
docker镜像层
位于下层的镜像称为父镜像(parrent image),最底层的称为基础镜像(base image);
最上层为“可读写”层,其下的均为“只读”层。
docker存储驱动
docker提供了多种存储驱动来实现不同的方式存储镜像,下面是常用的几种存储驱动:
- AUFS
- OverlayFS
- Devicemapper
- Btrfs
- VFS
AUFS
AUFS(AnotherUnionFS)是一种Union FS,是文件级的存储驱动。AUFS是一个能透明覆盖一个或多个现有文件系统的层状文件系统,把多层合并成文件系统的单层表示。简单来说就是支持将不同目录挂载到同一个虚拟文件系统下的文件系统。这种文件系统可以一层一层地叠加修改文件。无论底下有多少层都是只读的,只有最上层的文件系统是可写的。当需要修改一个文件时,AUFS创建该文件的一个副本,使用CoW将文件从只读层复制到可写层进行修改,结果也保存在可写层。在Docker中,底下的只读层就是image,可写层就是Container。
AUFS文件系统据说有3W行代码,而ext4文件系统却只有4000-5000行左右代码,这些代码是要被整合进内核的,后来AUFS申请要被合并进内核代码的时候,linuz觉得它这代码太过臃肿,于是拒绝了。因此AUFS这个文件系统一直以来就不是linux内核中自有的文件系统,想用AUFS这个文件系统的话,必须自己向内核打补丁并去编译使用它,但redhat系列的操作系统一向以稳定著称,不会干这种出格的事,所以在redhat系列操作系统中使用AUFS并无可能。而ubuntu上的docker默认使用的就是AUFS。
OverlayFS
Overlay是Linux内核3.18后支持的,也是一种Union FS,和AUFS的多层不同的是Overlay只有两层:一个upper文件系统和一个lower文件系统,分别代表Docker的镜像层和容器层。当需要修改一个文件时,使用CoW将文件从只读的lower复制到可写的upper进行修改,结果也保存在upper层。在Docker中,底下的只读层就是image,可写层就是Container。目前最新的OverlayFS为Overlay2。
AUFS和Overlay都是联合文件系统,但AUFS有多层,而Overlay只有两层,所以在做写时复制操作时,如果文件比较大且存在比较低的层,则AUSF会慢一些。而且Overlay并入了linux kernel mainline,AUFS没有。目前AUFS已基本被淘汰。
DeviceMapper
Device mapper是Linux内核2.6.9后支持的,提供的一种从逻辑设备到物理设备的映射框架机制,在该机制下,用户可以很方便的根据自己的需要制定实现存储资源的管理策略。AUFS和OverlayFS都是文件级存储,而Device mapper是块级存储,所有的操作都是直接对块进行操作,而不是文件。Device mapper驱动会先在块设备上创建一个资源池,然后在资源池上创建一个带有文件系统的基本设备,所有镜像都是这个基本设备的快照,而容器则是镜像的快照。所以在容器里看到文件系统是资源池上基本设备的文件系统的快照,并没有为容器分配空间。当要写入一个新文件时,在容器的镜像内为其分配新的块并写入数据,这个叫用时分配。当要修改已有文件时,再使用CoW为容器快照分配块空间,将要修改的数据复制到在容器快照中新的块里再进行修改。
OverlayFS是文件级存储,Device mapper是块级存储,当文件特别大而修改的内容很小,Overlay不管修改的内容大小都会复制整个文件,对大文件进行修改显然要比小文件要消耗更多的时间,而块级无论是大文件还是小文件都只复制需要修改的块,并不是整个文件,在这种场景下,显然device mapper要快一些。因为块级的是直接访问逻辑盘,适合IO密集的场景。而对于程序内部复杂,大并发但少IO的场景,Overlay的性能相对要强一些。
docker registry
启动容器时,docker daemon会试图从本地获取相关的镜像,本地镜像不存在时,其将从Registry中下载该镜像并保存到本地。
Registry用于保存docker镜像,包括镜像的层次结构和元数据。用户可以自建Registry,亦可使用官方的Docker Hub。
docker registry的分类:
-
Sponsor Registry:第三方的Registry,供客户和Docker社区使用
-
Mirror Registry:第三方的Registry,只让客户使用
-
Vendor Registry:由发布docker镜像的供应商提供的registry
-
Private Registry:通过设有防火墙和额外的安全层的私有实体提供的registry
docker registry的组成: -
Repository
- 由某特定的docker镜像的所有迭代版本组成的镜像仓库
- 一个Registry中可以存在多个Repository
- Repository可分为“顶层仓库”和“用户仓库”
- 用户仓库名称格式为“用户名/仓库名”
- 每个仓库可包含多个Tag(标签),每个标签对应一个镜像
-
Index
- 维护用户帐户、镜像的检验以及公共命名空间的信息
- 相当于为Registry提供了一个完成用户认证等功能的检索接口
Docker Registry中的镜像通常由开发人员制作,而后推送至“公共”或“私有”Registry上保存,供其他人员使用,例如“部署”到生产环境。
docker镜像的制作
多数情况下,我们做镜像是基于别人已存在的某个基础镜像来实现的,我们把它称为base image。比如一个纯净版的最小化的centos、ubuntu或debian。
那么这个最小化的centos镜像从何而来呢?其实这个基础镜像一般是由Docker Hub的相关维护人员,也就是Docker官方手动制作的。这个基础镜像的制作对于Docker官方的专业人员来说是非常容易的,但对于终端用户来说就不是那么容易制作的了。
Docker Hub
Docker Hub 是一种基于云的注册服务,它允许您链接到代码存储库、构建您的图像并测试它们、存储手动推送的图像以及链接到 Docker Cloud,以便您可以将图像部署到您的主机。
它为整个开发管道中的容器镜像发现、分发和变更管理、用户和团队协作以及工作流自动化提供了集中资源。
Docker Hub 提供以下主要功能:
- 镜像存储库
- 从社区和官方库中查找和拉取图像,并管理、推送到您有权访问的私有图像库和从中拉取。
- 自动化构建
- 更改源代码存储库时自动创建新图像。
- 钩子
- Webhooks 是自动构建的一项功能,可让您在成功推送到存储库后触发操作。
- 组织
- 创建工作组来管理对图像存储库的访问。
- GitHub 和 Bitbucket 集成
- 将 Hub 和您的 Docker 映像添加到您当前的工作流程中。
docker镜像的获取
要从远程注册表(例如您自己的 Docker 注册表)获取 Docker 镜像并将它们添加到您的本地系统,请使用 docker pull 命令:
# docker pull <registry>[:<port>]/[<namespace>/]<name>:<tag>
是在 TCP 上提供 docker-distribution 服务的主机(默认:5000)
一起,并识别由注册表控制的特定图像
- 一些注册中心也支持 raw ;对于那些,是可选的
- 然而,当它被包括在内时,提供的附加层次结构有助于区分具有相同特征的图像。
额外的层次结构
名称空间 | Examples(/) |
---|---|
organization | redhat/kubernetes, google/kubernetes |
login(username) | Alice/application, bob/application |
role | devel/database, test/database, prod/database |
镜像的生成
镜像的生成途径:
- Dockerfile
- 基于容器制作
- Docker Hub automated builds
基于容器制作镜像
从容器的更改创建一个新镜像
用法:
docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
Options | Default | Description |
---|---|---|
—author, -a | Author (e.g., “John Hannibal Smith hannibal@a-team.com”) | |
-c, --change list | Apply Dockerfile instruction to the created image | |
-m, --message string | Commit message | |
-p, --pause | true | Pause container during commit |
[root@localhost ~]# docker run -tid --name n1 --privileged=true centos /usr/sbin/init
8904ebdbae0c2b27b998f3cc2045737948506d36499b417d72326d713a8ec95d
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8904ebdbae0c centos "/usr/sbin/init" 7 seconds ago Up 5 seconds n1
[root@localhost ~]# docker exec -it n1 /bin/bash
[root@8904ebdbae0c /]# systemctl daemon-reload
[root@8904ebdbae0c /]#
[root@localhost ~]# docker cp /opt/nginx-1.20.2.tar.gz 8904ebdbae0c:/usr/src
[root@8904ebdbae0c /]# ls /usr/src/
debug kernels nginx-1.20.2.tar.gz
[root@8904ebdbae0c /]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make
[root@8904ebdbae0c /]# useradd -r -M -s /sbin/nologin nginx
[root@8904ebdbae0c /]# mkdir -p /var/log/nginx
[root@8904ebdbae0c /]# chown -R nginx.nginx /var/log/nginx
[root@8904ebdbae0c /]# cd /usr/src/
[root@8904ebdbae0c src]# tar xf nginx-1.20.2.tar.gz
[root@8904ebdbae0c src]# cd nginx-1.20.2
[root@8904ebdbae0c nginx-1.20.2]# ./configure \
> --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --with-debug \
> --with-http_ssl_module \
> --with-http_realip_module \
> --with-http_image_filter_module \
> --with-http_gunzip_module \
> --with-http_gzip_static_module \
> --with-http_stub_status_module \
> --http-log-path=/var/log/nginx/access.log \
> --error-log-path=/var/log/nginx/error.log
[root@8904ebdbae0c logs]# make && make install
[root@8904ebdbae0c logs]# vi /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[root@8904ebdbae0c logs]# systemctl daemon-reload
[root@8904ebdbae0c logs]# systemctl start nginx
[root@8904ebdbae0c logs]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
在创建镜像时,我们不能关闭容器,必须使其处于运行状态,所以我们必须要另起一个终端,然后执行
[root@localhost ~]# docker commit -p n1
sha256:67729a6d899865aead783a7ee07b3f3b7b0101189f04157aec6ae73885e2c278
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> <none> 67729a6d8998 8 seconds ago 550MB
xxkk/busybox v2.0 d733d8a3d541 2 hours ago 1.24MB
xxkk/busybox v1.0 42d4f23f7adc 3 hours ago 1.24MB
busybox latest d23834f29b38 2 days ago 1.24MB
httpd latest ad17c88403e2 13 days ago 143MB
nginx latest ea335eea17ab 2 weeks ago 141MB
hello-world latest feb5d9fea6a5 2 months ago 13.3kB
centos latest 5d0da3dc9764 2 months ago 231MB
[root@localhost ~]# docker tag 67729a6d8998 xxkk/nginx:1.0
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
xxkk/nginx 1.0 67729a6d8998 About a minute ago 550MB
xxkk/busybox v2.0 d733d8a3d541 2 hours ago 1.24MB
xxkk/busybox v1.0 42d4f23f7adc 3 hours ago 1.24MB
busybox latest d23834f29b38 2 days ago 1.24MB
httpd latest ad17c88403e2 13 days ago 143MB
nginx latest ea335eea17ab 2 weeks ago 141MB
hello-world latest feb5d9fea6a5 2 months ago 13.3kB
centos latest 5d0da3dc9764 2 months ago 231MB
[root@localhost ~]# docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: xxkk
Password:
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
[root@localhost ~]# docker push xxkk/nginx:1.0
The push refers to repository [docker.io/xxkk/nginx]
0a855b5be1a2: Pushed
74ddd0ec08fa: Mounted from library/centos
1.0: digest: sha256:e95565fa5d36d2b7d949455a87dd6d7f04c5d522e1788e821c1ceba4259b390e size: 742
使用新生成的镜像创建容器
[root@localhost ~]# docker run -tid --name n2 --privileged=true xxkk/nginx:1.0 /usr/sbin/init
866c93031c67c04221d9fb44282f24159109b61924cbbd7ec47c83de46e05509
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
866c93031c67 xxkk/nginx:1.0 "/usr/sbin/init" 6 seconds ago Up 5 seconds n2
8904ebdbae0c centos "/usr/sbin/init" 50 minutes ago Up 50 minutes n1
[root@localhost ~]# docker exec -it 866c93031c67 /bin/bash
[root@866c93031c67 /]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
[root@866c93031c67 /]# systemctl start nginx
[root@866c93031c67 /]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
由此可见,新生成的镜像中是包含了新增的内容的
使用docker inspect命令查看n2容器启动的默认进程是什么,以及其IP地址,然后用curl命令访问该IP,看是否能访问到网页
[root@localhost ~]# docker inspect n2
"Cmd": [
"/usr/sbin/init"
],
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.3",
[root@localhost ~]# curl 172.17.0.3
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
镜像的导入与导出
假如有2台主机,我们在主机1上做了一个镜像,主机2想用这个镜像怎么办呢?
我们可以在主机1上push镜像到镜像仓库中,然后在主机2上pull把镜像拉下来使用,这种方式就显得比较麻烦,假如我只是测试用的,在一台主机上做好镜像后在另一台主机上跑一下就行了,没必要推到仓库上然后又把它拉到本地来。
此时我们可以在已有镜像的基础上把镜像打包成一个压缩文件,然后拷贝到另一台主机上将其导入,这就是镜像的导入和导出功能。
docker中我们使用docker save进行导出,使用docker load进行导入。
在已生成镜像的主机上执行docker save导出镜像
[root@localhost ~]# docker save -o nginx.gz xxkk/nginx:1.0
[root@localhost ~]# ls
anaconda-ks.cfg nginx.gz
[root@localhost ~]#
在另一台没有镜像的主机上执行docker load导入镜像
[root@localhost ~]# docker load -i nginx.gz