Docker--镜像结构

一.Docker镜像的简单概述

Docker镜像是一个只读的Docker容器模板,含有启动Docker容器所需的文件系统结构及其内容,因此是启动一个Docker容器的基础。

Docker镜像的文件内容以及一些运行Docker容器的配置文件组成了Docker容器的静态文件系统运行环境–rootfs。可以这么理解,Docker镜像是Docker容器的静态视角,Docker容器是Docker镜像的运行状态

二.镜像的分层结构
特点
  • 1.共享宿主机的内核
  • 2.base镜像提供的是最小的linux发行版
    其实就是linux的根/文件系统
  • 3.同一docker主机支持运行多种linux发行版
  • 4.采用分层结构的做大好处是“共享资源”
    不同的镜像使用同一个镜像层的时候,文件系统便可以实现资源共享,只需要备份一次,节省空间。
    在这里插入图片描述
查看分层结构的命令

docker load -i 镜像名 #拉取镜像

[root@toto6 images]# docker load -i ubuntu.tar    # 拉取本地第一个镜像
56abdd66ba31: Loading layer  196.8MB/196.8MB
9468150a390c: Loading layer  208.9kB/208.9kB
11083b444c90: Loading layer  4.608kB/4.608kB
5f70bf18a086: Loading layer  1.024kB/1.024kB
Loaded image: ubuntu:latest
[root@toto6 images]# docker load -i rhel7.tar     # 拉取本地的第二个镜像
e1f5733f050b: Loading layer  147.1MB/147.1MB
[root@toto6 images]# docker load -i busybox.tar    # 拉取本地的第三个镜像
8a788232037e: Loading layer   1.37MB/1.37MB
Loaded image: busybox:latest
[root@toto6 images]#

docker images #查看所有已经拉取的镜像

[root@toto6 images]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
busybox             latest              59788edf1f3e        9 months ago        1.15MB
game2048            latest              19299002fdbe        2 years ago         55.5MB
ubuntu              latest              07c86167cdc4        3 years ago         188MB
rhel7               latest              0a3eb3fde7fd        5 years ago         140MB

dcoker history 镜像名称 # 查看特定进行的构造,一层一层

[root@toto6 images]# docker history  ubuntu:latest 
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
07c86167cdc4        3 years ago         /bin/sh -c #(nop) CMD ["/bin/bash"]             0B                  
<missing>           3 years ago         /bin/sh -c sed -i 's/^#\s*\(deb.*universe\)$…   1.9kB               
<missing>           3 years ago         /bin/sh -c echo '#!/bin/sh' > /usr/sbin/poli…   195kB               
<missing>           3 years ago         /bin/sh -c #(nop) ADD file:b9504126dc5590898…   188MB               
使用镜像开启并运行一个容器

镜像作为开启容器的模板。使用镜像创建并且运行一个容器,就是在镜像层的最上层添加一层容器层。所有的镜像层都是只读的容器层是可写的,容器开启之后,所有的修改该作都保存在可写的容器层。

容器以下所有镜像曾都是只读的,docker查找文件时是从上往下依次查找;容器层用来保存镜像变化的部分,并不会对镜像本身进行任何修改;一个镜像最多可以有127层

docker容器进行可写时,是在可写容器层进行可写动作,经底层镜像层的文件会先复制到可写层再执行删除、创建等动作

在这里插入图片描述

使用镜像创建并运行一个容器:

[root@toto6 images]# docker run -it --name vm1 ubuntu
#-it  交互式   --name 容器名称   
root@927ae4d1dca7:/# uname -r  # 查看容器的内核版本
3.10.0-514.el7.x86_64   # 显示
root@927ae4d1dca7:/# touch /mnt/file{1..5}    # 在容器中创建文件。写入内容
root@927ae4d1dca7:/# ls /mnt/
file1  file2  file3  file4  file5   #成功创建5个文件

退出该容器查看系统的内核版本

[root@toto6 images]# uname -r
3.10.0-514.el7.x86_64   # 容器使用的是该宿主机的内核

清除非活跃的容器:
docker container prune

[root@toto6 images]# docker container prune 
WARNING! This will remove all stopped containers.
Are you sure you want to continue? [y/N] y
Deleted Containers:
927ae4d1dca7b75ef588328803b20c2eab9ca2d22102a57fba51621a1b53503c

Total reclaimed space: 40B

再次使用上次使用镜像创建并运行一个容器

[root@toto6 images]# docker run -it --name vm2 ubuntu 
root@5c69f6454acb:/# ls /mnt/  
root@5c69f6454acb:/#  
  # 这里并没有上次创建的文件,是因为上次的操作全部写在容器层,镜像层的内容只读。
  当上次的容器释放之后,写在容器层的内容也会随之消失。
将创建的容器重新打包为一个镜像

在容器中进行创建文件:

root@5c69f6454acb:/# touch  /mnt/file{1..5}
root@5c69f6454acb:/# ls /mnt/
file1  file2  file3  file4  file5  
root@5c69f6454acb:/# exit

将该容器重新打包成一个镜像:

[root@toto6 images]# docker ps -a    # 查看容器
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
5c69f6454acb        ubuntu              "/bin/bash"         5 minutes ago       Exited (0) 23 seconds ago                       vm2
[root@toto6 images]# docker commit vm2 ubuntu:v2    # 将该才的容器打包成一个新的镜像
sha256:ceff04ff5225ffcda3f5f1ce7c08d25d13c67bf6c4bcccec1c57b90994b8b2de
[root@toto6 images]# docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              v2                  ceff04ff5225        11 seconds ago      188MB   # 新生成的镜像
busybox             latest              59788edf1f3e        9 months ago        1.15MB
game2048            latest              19299002fdbe        2 years ago         55.5MB
ubuntu              latest              07c86167cdc4        3 years ago         188MB
rhel7               latest              0a3eb3fde7fd        5 years ago         140MB

使用容器创建镜像。容器就是在镜像层的最上方存在一个可写的容器层。将容器打包镜像,就是将该可写的容器层,变成一个只读的镜像层,和下层的所有镜像层一起作为新镜像的镜像层。

以上例子中镜像ubuntu:v2 是在ubuntu镜像打开容器的基础上生成的。查看他们的镜像分层。

[root@toto6 images]# docker history  ubuntu:v2
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
ceff04ff5225        7 minutes ago       /bin/bash                                       41B                 
07c86167cdc4        3 years ago         /bin/sh -c #(nop) CMD ["/bin/bash"]             0B                  
<missing>           3 years ago         /bin/sh -c sed -i 's/^#\s*\(deb.*universe\)$…   1.9kB               
<missing>           3 years ago         /bin/sh -c echo '#!/bin/sh' > /usr/sbin/poli…   195kB               
<missing>           3 years ago         /bin/sh -c #(nop) ADD file:b9504126dc5590898…   188MB               

[root@toto6 images]# docker history  ubuntu
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
07c86167cdc4        3 years ago         /bin/sh -c #(nop) CMD ["/bin/bash"]             0B                  
<missing>           3 years ago         /bin/sh -c sed -i 's/^#\s*\(deb.*universe\)$…   1.9kB               
<missing>           3 years ago         /bin/sh -c echo '#!/bin/sh' > /usr/sbin/poli…   195kB               
<missing>           3 years ago         /bin/sh -c #(nop) ADD file:b9504126dc5590898…   188MB  
ubuntu:v2 是在ubuntu镜像基础上多添加了一层。

删除容器:

[root@toto6 images]# docker rm vm2
vm2

删除镜像:

[root@toto6 images]# docker image rm ubuntu:v2
Untagged: ubuntu:v2
Deleted: sha256:ceff04ff5225ffcda3f5f1ce7c08d25d13c67bf6c4bcccec1c57b90994b8b2de
Deleted: sha256:16686d7c5eec38ab61dde622a52e0598e8406b5cc220c6a089756dd8f076f6ab
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值