docker联合文件系统(UnionFS)

学习docker你可能有一下几个疑惑

kubernetes基础之docker的目录是如何“拼”起来的---联合文件系统(UnionFS)_docker


  • 又镜像生成的容器之间到底有什么关系呢
带着疑惑请往下看,本篇将解密一切
一个容器到底是由镜像怎么生成的,同一个镜像多个容器之间的文件有什么关联

开始你的表演

第一、验证一下,同一个镜像创建多个容器,观察下磁盘的大小是否变化
[root@localhost ~]# docker pull nginx    #拉取一个nginx镜像
Using default tag: latest
latest: Pulling from library/nginx
a076a628af6f: Pull complete 
0732ab25fa22: Pull complete 
d7f36f6fe38f: Pull complete 
f72584a26f32: Pull complete 
7125e4df9063: Pull complete 
Digest: sha256:10b8cc432d56da8b61b070f4c7d2543a9ed17c2b23010b43af434fd40e2ca4aa
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
[root@localhost ~]# docker images   #可以看到当前有一个nginx的镜像
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
nginx        latest    f6d0b4767a6c   7 weeks ago   133MB   #可以看到这个镜像有133M

[root@localhost ~]# df -h
文件系统                 容量  已用  可用 已用% 挂载点
/dev/mapper/centos-root   17G  2.0G   15G   12% /
devtmpfs                 476M     0  476M    0% /dev
tmpfs                    488M     0  488M    0% /dev/shm
tmpfs                    488M  7.7M  480M    2% /run
tmpfs                    488M     0  488M    0% /sys/fs/cgroup
/dev/sda1               1014M  130M  885M   13% /boot
tmpfs                     98M     0   98M    0% /run/user/0

#接下来我们启动more more多的容器看看
[root@localhost ~]# docker run -d nginx
6f3e3f11e0a4d248c9c578608ed1027a714c5190547b4d18a7674caffcd53a79
[root@localhost ~]# docker run -d nginx
e5e18a18ae4b31bc797d28a6542a7687935e1137da8887c3f7270886f521734d
^[[A[root@localhost ~]# docker run -d nginx
7001daddfbdfd879bdbaf5b31f669e60a1952167c99c649d9d24a8799dddfe12
^[[A[root@localhost ~]# docker run -d nginx
6c1f7b3e03933238fa1f3ddb06acb74f85f5325d359e047fdf884711caeab3d8
[root@localhost ~]# docker run -d nginx
dbf9b9fe043e4a1f209570dd4792d375ceaabd1c22d99395d5adbcee307076d8
[root@localhost ~]# docker run -d nginx
433ce7a40a12fafcf03d913ad757ce20e1d78d9dc777a98ca4df84710a09c67f
[root@localhost ~]# docker run -d nginx
ce2693bbb5ad9fdb6e976fcaa4919f526030b4ecdc09528640bda9d73afa123d
[root@localhost ~]# docker run -d nginx
d6f7ef40c825403795d6ece4a9a25f3158ee43a6ac18fc35ada1f77938ad9824
[root@localhost ~]# docker run -d nginx
08de3c93f339d249f95a6e427430c633320cdaff4e31e35447ec5bf284343f61
[root@localhost ~]# docker run -d nginx
4bc994d253ac82d27d15d9db9732d8576ed52e54be468cf316fdeaf7a12ccb63
[root@localhost ~]# docker run -d nginx
e0149592d0516092879805fbd5198bfd49f85f5ce0842981927c1f5c928d3abe
[root@localhost ~]# docker run -d nginx
378ccf0c81a809b5e1ba1d6baa34e27359c672c297caf9e5fea2989cfc4b0dc6
[root@localhost ~]# docker run -d nginx
18afe1e59f03508d2d1b1f163b893a3deea54a838f9dd3f8c2a5e7128cd65bfb
[root@localhost ~]# docker run -d nginx
1c45086a73f9572ed467982a44c5865f883734936783801fe5164af8fe0d6487
[root@localhost ~]# docker ps -a   #看到有好多的容器在运行中
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS     NAMES
1c45086a73f9   nginx     "/docker-entrypoint.…"   9 seconds ago    Up 8 seconds    80/tcp    eloquent_gates
18afe1e59f03   nginx     "/docker-entrypoint.…"   10 seconds ago   Up 9 seconds    80/tcp    stupefied_payne
378ccf0c81a8   nginx     "/docker-entrypoint.…"   11 seconds ago   Up 11 seconds   80/tcp    quizzical_cartwright
e0149592d051   nginx     "/docker-entrypoint.…"   12 seconds ago   Up 12 seconds   80/tcp    recursing_almeida
4bc994d253ac   nginx     "/docker-entrypoint.…"   14 seconds ago   Up 13 seconds   80/tcp    affectionate_margulis
08de3c93f339   nginx     "/docker-entrypoint.…"   15 seconds ago   Up 14 seconds   80/tcp    naughty_solomon
d6f7ef40c825   nginx     "/docker-entrypoint.…"   16 seconds ago   Up 15 seconds   80/tcp    nifty_proskuriakova
ce2693bbb5ad   nginx     "/docker-entrypoint.…"   17 seconds ago   Up 16 seconds   80/tcp    magical_panini
433ce7a40a12   nginx     "/docker-entrypoint.…"   19 seconds ago   Up 18 seconds   80/tcp    intelligent_montalcini
dbf9b9fe043e   nginx     "/docker-entrypoint.…"   20 seconds ago   Up 19 seconds   80/tcp    nifty_visvesvaraya
6c1f7b3e0393   nginx     "/docker-entrypoint.…"   21 seconds ago   Up 20 seconds   80/tcp    cranky_mirzakhani
7001daddfbdf   nginx     "/docker-entrypoint.…"   22 seconds ago   Up 21 seconds   80/tcp    bold_heisenberg
e5e18a18ae4b   nginx     "/docker-entrypoint.…"   23 seconds ago   Up 22 seconds   80/tcp    lucid_banzai
6f3e3f11e0a4   nginx     "/docker-entrypoint.…"   27 seconds ago   Up 25 seconds   80/tcp    recursing_brown

[root@localhost ~]# df -h   #可以看到系统磁盘大小还是15G
文件系统                 容量  已用  可用 已用% 挂载点
/dev/mapper/centos-root   17G  2.0G   15G   12% /
devtmpfs                 476M     0  476M    0% /dev
tmpfs                    488M     0  488M    0% /dev/shm
tmpfs                    488M  8.6M  479M    2% /run
tmpfs                    488M     0  488M    0% /sys/fs/cgroup
/dev/sda1               1014M  130M  885M   13% /boot   ###大家要记住下面的这些东西,一会你们聚会恍然大明白了
tmpfs                     98M     0   98M    0% /run/user/0
overlay                   17G  2.0G   15G   12%        /var/lib/docker/overlay2/b2d0837433350e7454b4e0915127900fc04de1535a9af83404ce94c060df1a54/merged
overlay                   17G  2.0G   15G   12% /var/lib/docker/overlay2/0a8e70f1dc817e574bf3bfccca8ded0f7ceb0a856aff6b259038ebf69649cc66/merged
overlay                   17G  2.0G   15G   12% /var/lib/docker/overlay2/162d59345ca6b4867e2572a1763825952a6212fbc114aad17f4cdfd71191262c/merged
overlay                   17G  2.0G   15G   12% /var/lib/docker/overlay2/4979f6e401872c59a4744558b05f8f86c51bf73cba3e632802ac05c2ce9dceee/merged
overlay                   17G  2.0G   15G   12% /var/lib/docker/overlay2/5e78ced6812cb269b5cc4b3ac9c03a60d2b6cba3edcfb98908971b4996edacd0/merged
overlay                   17G  2.0G   15G   12% /var/lib/docker/overlay2/3a07c83675080b755f662dd181e2062de7142f653eb23cbed312b4fdea78115d/merged
overlay                   17G  2.0G   15G   12% /var/lib/docker/overlay2/f9ffcc3d3a41c9fce77ffc8c5d46b73fd381ac8e3f9fb2352fd6d2c121a892a2/merged
overlay                   17G  2.0G   15G   12% /var/lib/docker/overlay2/3111318b0b752d2500443263cb9576078d934a473ddab78b2c0faf1d2f039f57/merged
overlay                   17G  2.0G   15G   12% /var/lib/docker/overlay2/bd0b83f999d2f17527b9812f608c54a63b5fe3f6df6869b6cc386a38d6058c91/merged
overlay                   17G  2.0G   15G   12% /var/lib/docker/overlay2/503ac746b9f64bb750f953a09282ac146a2a1329caadad36fb846e2ddb564534/merged
overlay                   17G  2.0G   15G   12% /var/lib/docker/overlay2/423f6191634174850ddd91ecbb3cce3fad6245f8ace09d8ec84a66e3a5ed9258/merged
overlay                   17G  2.0G   15G   12% /var/lib/docker/overlay2/dbb5c551bf81d3ea91d39504d4cd53016ff5f699202f0576a1b6ce8a7c513d99/merged
overlay                   17G  2.0G   15G   12% /var/lib/docker/overlay2/1b4d82471b99662962adeb0892f8708da7840d775fd6dfc37a118b7c11f05af9/merged
overlay                   17G  2.0G   15G   12% /var/lib/docker/overlay2/080e4cbbcc4082cbe07ae6d5c22f63b0bd9e8caada21d46783097983747c2e34/merged
[root@localhost ~]# 

事实证明,有一个镜像创建的多个容器不会对服务器磁盘有太大的影响
第二、解剖docker。了解docker内部的世界
[root@localhost ~]# docker ps -aq | xargs docker stop | xargs docker rm  ##借助两个管道符先将容器清理一下
1c45086a73f9
18afe1e59f03
378ccf0c81a8
e0149592d051
4bc994d253ac
08de3c93f339
d6f7ef40c825
ce2693bbb5ad
433ce7a40a12
dbf9b9fe043e
6c1f7b3e0393
7001daddfbdf
e5e18a18ae4b
6f3e3f11e0a4
[root@localhost ~]# docker ps -a   #可以看到没有容器了
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

##接下来我们创建两个测试容器,分别是web1和web2方便区分


[root@localhost ~]# docker run -d --name=web1 nginx   #创建web1容器
afb553ce70c8ac196a810ea1516a866704285cdb7052d9921d197b8f869af9ba
[root@localhost ~]# docker run -d --name=web2 nginx  #创建web2容器
be9a8ab117d1386e9ebf67464a039388ca679e304eb00c32b33b15d50a2268be

[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS     NAMES
be9a8ab117d1   nginx     "/docker-entrypoint.…"   25 seconds ago   Up 24 seconds   80/tcp    web2
afb553ce70c8   nginx     "/docker-entrypoint.…"   30 seconds ago   Up 29 seconds   80/tcp    web1

##然后分别在这两个容器里面创建几个文件或目录
[root@localhost ~]# docker exec -it afb553ce70c8 bash
root@afb553ce70c8:/# ls
bin   dev		   docker-entrypoint.sh  home  lib64  mnt  proc  run   srv  tmp  var
boot  docker-entrypoint.d  etc			 lib   media  opt  root  sbin  sys  usr
root@afb553ce70c8:/# mkdir a aa aaa
root@afb553ce70c8:/# touch a.txt aa.txt aaa.txt
root@afb553ce70c8:/# ls
a      aa      aaa	bin   dev		   docker-entrypoint.sh  home  lib64  mnt  proc  run   srv  tmp  var
a.txt  aa.txt  aaa.txt	boot  docker-entrypoint.d  etc			 lib   media  opt  root  sbin  sys  usr
root@afb553ce70c8:/# exit
exit

[root@localhost ~]# docker exec -it be9a8ab117d1 bash
root@be9a8ab117d1:/# mkdir b bb bbb
root@be9a8ab117d1:/# touch b.txt bb.txt bbb.txt 
root@be9a8ab117d1:/# ls
b      bb      bbb	bin   dev		   docker-entrypoint.sh  home  lib64  mnt  proc  run   srv  tmp  var
b.txt  bb.txt  bbb.txt	boot  docker-entrypoint.d  etc			 lib   media  opt  root  sbin  sys  usr
root@be9a8ab117d1:/# exit
exit

##创建完成之后,让我们来看看这两个容器的详细信息
[root@localhost ~]# docker inspect web1 | egrep -i data -A 20 -B 1   #web1的目录使用详情
        "GraphDriver": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/134e38f2b15f44335cfa68fe524f0ebedaaf9e058ab2a1997955a0a4db9483fa-init/diff:/var/lib/docker/overlay2/9ea913b7611a03e5e6ff79e4ad5966323ddc8d22930fd538b8f4d0cbd8adc6ad/diff:/var/lib/docker/overlay2/4ce0979bb143d9e44e3bab1a8827aec82af2124490824dcd0eded2bdea025543/diff:/var/lib/docker/overlay2/727f637b61409b8760394186a49582dc1bc497a0433074b26fdba053d6263d2b/diff:/var/lib/docker/overlay2/5dbf8694ae45b3211108042719d6ecee8a2aa2c6f59db2e9c7d8a374b5e7e804/diff:/var/lib/docker/overlay2/db778a93aefbc70fd58665217197361951cd31f7edf7a89b1e6f222d9a5e85d6/diff",
                "MergedDir": "/var/lib/docker/overlay2/134e38f2b15f44335cfa68fe524f0ebedaaf9e058ab2a1997955a0a4db9483fa/merged",
                "UpperDir": "/var/lib/docker/overlay2/134e38f2b15f44335cfa68fe524f0ebedaaf9e058ab2a1997955a0a4db9483fa/diff",
                "WorkDir": "/var/lib/docker/overlay2/134e38f2b15f44335cfa68fe524f0ebedaaf9e058ab2a1997955a0a4db9483fa/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [],
        "Config": {
            "Hostname": "afb553ce70c8",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "80/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,


[root@localhost ~]# docker inspect web2 | egrep -i data -A 20 -B 1   #web2目录的使用详情
        "GraphDriver": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/2c609b1870a4e9da2c2cdb2f411827250aeebb42a41d165e21caeb1864a10459-init/diff:/var/lib/docker/overlay2/9ea913b7611a03e5e6ff79e4ad5966323ddc8d22930fd538b8f4d0cbd8adc6ad/diff:/var/lib/docker/overlay2/4ce0979bb143d9e44e3bab1a8827aec82af2124490824dcd0eded2bdea025543/diff:/var/lib/docker/overlay2/727f637b61409b8760394186a49582dc1bc497a0433074b26fdba053d6263d2b/diff:/var/lib/docker/overlay2/5dbf8694ae45b3211108042719d6ecee8a2aa2c6f59db2e9c7d8a374b5e7e804/diff:/var/lib/docker/overlay2/db778a93aefbc70fd58665217197361951cd31f7edf7a89b1e6f222d9a5e85d6/diff",
                "MergedDir": "/var/lib/docker/overlay2/2c609b1870a4e9da2c2cdb2f411827250aeebb42a41d165e21caeb1864a10459/merged",
                "UpperDir": "/var/lib/docker/overlay2/2c609b1870a4e9da2c2cdb2f411827250aeebb42a41d165e21caeb1864a10459/diff",
                "WorkDir": "/var/lib/docker/overlay2/2c609b1870a4e9da2c2cdb2f411827250aeebb42a41d165e21caeb1864a10459/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [],
        "Config": {
            "Hostname": "be9a8ab117d1",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "80/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,

开始对比这两个容器所用目录的差别

[root@localhost ~]# docker inspect web1 | egrep -i data -A 20 -B 1 > web1.txt
[root@localhost ~]# docker inspect web2 | egrep -i data -A 20 -B 1 > web2.txt
[root@localhost ~]# which diff
/usr/bin/diff
[root@localhost ~]# diff -c web1.txt web2.txt 
*** web1.txt	2021-03-04 23:48:36.974208148 +0800
--- web2.txt	2021-03-04 23:48:45.002225087 +0800
***************
*** 1,15 ****
          "GraphDriver": {
              "Data": {
!                 "LowerDir": "/var/lib/docker/overlay2/134e38f2b15f44335cfa68fe524f0ebedaaf9e058ab2a1997955a0a4db9483fa-init/diff:/var/lib/docker/overlay2/9ea913b7611a03e5e6ff79e4ad5966323ddc8d22930fd538b8f4d0cbd8adc6ad/diff:/var/lib/docker/overlay2/4ce0979bb143d9e44e3bab1a8827aec82af2124490824dcd0eded2bdea025543/diff:/var/lib/docker/overlay2/727f637b61409b8760394186a49582dc1bc497a0433074b26fdba053d6263d2b/diff:/var/lib/docker/overlay2/5dbf8694ae45b3211108042719d6ecee8a2aa2c6f59db2e9c7d8a374b5e7e804/diff:/var/lib/docker/overlay2/db778a93aefbc70fd58665217197361951cd31f7edf7a89b1e6f222d9a5e85d6/diff",
!                 "MergedDir": "/var/lib/docker/overlay2/134e38f2b15f44335cfa68fe524f0ebedaaf9e058ab2a1997955a0a4db9483fa/merged",
!                 "UpperDir": "/var/lib/docker/overlay2/134e38f2b15f44335cfa68fe524f0ebedaaf9e058ab2a1997955a0a4db9483fa/diff",
!                 "WorkDir": "/var/lib/docker/overlay2/134e38f2b15f44335cfa68fe524f0ebedaaf9e058ab2a1997955a0a4db9483fa/work"
              },
              "Name": "overlay2"
          },
          "Mounts": [],
          "Config": {
!             "Hostname": "afb553ce70c8",
              "Domainname": "",
              "User": "",
              "AttachStdin": false,
--- 1,15 ----
          "GraphDriver": {
              "Data": {
!                 "LowerDir": "/var/lib/docker/overlay2/2c609b1870a4e9da2c2cdb2f411827250aeebb42a41d165e21caeb1864a10459-init/diff:/var/lib/docker/overlay2/9ea913b7611a03e5e6ff79e4ad5966323ddc8d22930fd538b8f4d0cbd8adc6ad/diff:/var/lib/docker/overlay2/4ce0979bb143d9e44e3bab1a8827aec82af2124490824dcd0eded2bdea025543/diff:/var/lib/docker/overlay2/727f637b61409b8760394186a49582dc1bc497a0433074b26fdba053d6263d2b/diff:/var/lib/docker/overlay2/5dbf8694ae45b3211108042719d6ecee8a2aa2c6f59db2e9c7d8a374b5e7e804/diff:/var/lib/docker/overlay2/db778a93aefbc70fd58665217197361951cd31f7edf7a89b1e6f222d9a5e85d6/diff",
!                 "MergedDir": "/var/lib/docker/overlay2/2c609b1870a4e9da2c2cdb2f411827250aeebb42a41d165e21caeb1864a10459/merged",
!                 "UpperDir": "/var/lib/docker/overlay2/2c609b1870a4e9da2c2cdb2f411827250aeebb42a41d165e21caeb1864a10459/diff",
!                 "WorkDir": "/var/lib/docker/overlay2/2c609b1870a4e9da2c2cdb2f411827250aeebb42a41d165e21caeb1864a10459/work"
              },
              "Name": "overlay2"
          },
          "Mounts": [],
          "Config": {
!             "Hostname": "be9a8ab117d1",
              "Domainname": "",
              "User": "",
              "AttachStdin": false,

kubernetes基础之docker的目录是如何“拼”起来的---联合文件系统(UnionFS)_kubernetes_02

详解:

kubernetes基础之docker的目录是如何“拼”起来的---联合文件系统(UnionFS)_2d_03

到这里的话,大家是不是大概脑子中是不是有个构思了,就是说容器在创建的时候,里面的目录文件是拼接出来的,为了验证一下这个想法我们继续向下走

了解联合文件系统

root@localhost ~]# docker exec -it web1 bash   #再看下web1容器的目录
root@afb553ce70c8:/# ls
a  a.txt  aa  aa.txt  aaa  aaa.txt  bin  boot  dev  docker-entrypoint.d  docker-entrypoint.sh  etc  home  lib  lib64  media  mnt  opt  proc  root  run	sbin  srv  sys	tmp  usr  var
root@afb553ce70c8:/# ls docker-entrypoint.d/  #查看这个初始化目录里面都有啥
10-listen-on-ipv6-by-default.sh  20-envsubst-on-templates.sh

###从容器的详细信息里面,按个目录查看一下
[root@localhost ~]# docker inspect web1 | egrep -i data -A 20 -B 1
        "GraphDriver": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/134e38f2b15f44335cfa68fe524f0ebedaaf9e058ab2a1997955a0a4db9483fa-init/diff:/var/lib/docker/overlay2/9ea913b7611a03e5e6ff79e4ad5966323ddc8d22930fd538b8f4d0cbd8adc6ad/diff:/var/lib/docker/overlay2/4ce0979bb143d9e44e3bab1a8827aec82af2124490824dcd0eded2bdea025543/diff:/var/lib/docker/overlay2/727f637b61409b8760394186a49582dc1bc497a0433074b26fdba053d6263d2b/diff:/var/lib/docker/overlay2/5dbf8694ae45b3211108042719d6ecee8a2aa2c6f59db2e9c7d8a374b5e7e804/diff:/var/lib/docker/overlay2/db778a93aefbc70fd58665217197361951cd31f7edf7a89b1e6f222d9a5e85d6/diff",
                "MergedDir": "/var/lib/docker/overlay2/134e38f2b15f44335cfa68fe524f0ebedaaf9e058ab2a1997955a0a4db9483fa/merged",
                "UpperDir": "/var/lib/docker/overlay2/134e38f2b15f44335cfa68fe524f0ebedaaf9e058ab2a1997955a0a4db9483fa/diff",
                "WorkDir": "/var/lib/docker/overlay2/134e38f2b15f44335cfa68fe524f0ebedaaf9e058ab2a1997955a0a4db9483fa/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [],
        "Config": {
            "Hostname": "afb553ce70c8",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "80/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
##以下都是 "LowerDir目录里面的
[root@localhost ~]# ls /var/lib/docker/overlay2/9ea913b7611a03e5e6ff79e4ad5966323ddc8d22930fd538b8f4d0cbd8adc6ad/diff
docker-entrypoint.d
[root@localhost ~]# ls /var/lib/docker/overlay2/9ea913b7611a03e5e6ff79e4ad5966323ddc8d22930fd538b8f4d0cbd8adc6ad/diff/docker-entrypoint.d/
20-envsubst-on-templates.sh
[root@localhost ~]# ls /var/lib/docker/overlay2/4ce0979bb143d9e44e3bab1a8827aec82af2124490824dcd0eded2bdea025543/diff
docker-entrypoint.d
[root@localhost ~]# ls /var/lib/docker/overlay2/4ce0979bb143d9e44e3bab1a8827aec82af2124490824dcd0eded2bdea025543/diff/docker-entrypoint.d/
10-listen-on-ipv6-by-default.sh
[root@localhost ~]# ls /var/lib/docker/overlay2/727f637b61409b8760394186a49582dc1bc497a0433074b26fdba053d6263d2b/diff
docker-entrypoint.sh
[root@localhost ~]# ls /var/lib/docker/overlay2/5dbf8694ae45b3211108042719d6ecee8a2aa2c6f59db2e9c7d8a374b5e7e804/diff
docker-entrypoint.d  etc  lib  tmp  usr  var
[root@localhost ~]# ls /var/lib/docker/overlay2/db778a93aefbc70fd58665217197361951cd31f7edf7a89b1e6f222d9a5e85d6/diff
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
##这个是"MergedDir"目录里面的内容
[root@localhost ~]# ls /var/lib/docker/overlay2/134e38f2b15f44335cfa68fe524f0ebedaaf9e058ab2a1997955a0a4db9483fa/merged
a  aa  aaa  aaa.txt  aa.txt  a.txt  bin  boot  dev  docker-entrypoint.d  docker-entrypoint.sh  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
 #这个是"UpperDir"目录里面的内容
[root@localhost ~]# ls /var/lib/docker/overlay2/134e38f2b15f44335cfa68fe524f0ebedaaf9e058ab2a1997955a0a4db9483fa/diff
a  aa  aaa  aaa.txt  aa.txt  a.txt  etc  root  run  var

通过上面的对比大家可以发现一下几点

  • "MergedDir目录就是容器里面的当前目录一模一样
  • "LowerDir里面的多个目录下面的文件目录整理在一起的话就是我们在没有创建文件a.txt aa.txt aaa.txt和目录a aa aaa时容器的目录
  • "UpperDir"这个目录里面就是我们创建文件a.txt aa.txt aaa.txt和目录a aa aaa的目录,当然了还有人会问这个目录下面还有etc root run var ,为什么,因为你在创建文件和目录的时候这里面有些东西发生了变化
  • "UpperDir" + "LowerDir = "MergedDir,小伙子很聪明,看到这里大概也知道了容器的目录是怎么拼起来的吧

让我们来看看web2是不是和我们所说的一样

让我们直接看web2的"UpperDir"吧,,其他的大家自行验证
[root@localhost ~]# docker inspect web2 | grep -i data -A20 -B 1
        "GraphDriver": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/2c609b1870a4e9da2c2cdb2f411827250aeebb42a41d165e21caeb1864a10459-init/diff:/var/lib/docker/overlay2/9ea913b7611a03e5e6ff79e4ad5966323ddc8d22930fd538b8f4d0cbd8adc6ad/diff:/var/lib/docker/overlay2/4ce0979bb143d9e44e3bab1a8827aec82af2124490824dcd0eded2bdea025543/diff:/var/lib/docker/overlay2/727f637b61409b8760394186a49582dc1bc497a0433074b26fdba053d6263d2b/diff:/var/lib/docker/overlay2/5dbf8694ae45b3211108042719d6ecee8a2aa2c6f59db2e9c7d8a374b5e7e804/diff:/var/lib/docker/overlay2/db778a93aefbc70fd58665217197361951cd31f7edf7a89b1e6f222d9a5e85d6/diff",
                "MergedDir": "/var/lib/docker/overlay2/2c609b1870a4e9da2c2cdb2f411827250aeebb42a41d165e21caeb1864a10459/merged",
                "UpperDir": "/var/lib/docker/overlay2/2c609b1870a4e9da2c2cdb2f411827250aeebb42a41d165e21caeb1864a10459/diff",
                "WorkDir": "/var/lib/docker/overlay2/2c609b1870a4e9da2c2cdb2f411827250aeebb42a41d165e21caeb1864a10459/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [],
        "Config": {
            "Hostname": "be9a8ab117d1",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "80/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
[root@localhost ~]# ls /var/lib/docker/overlay2/2c609b1870a4e9da2c2cdb2f411827250aeebb42a41d165e21caeb1864a10459/diff
b  bb  bbb  bbb.txt  bb.txt  b.txt  etc  root  run  var   ##就是我们自己创建的文件和目录,验证完毕

解决篇前的疑问

  • 为什么创建那么多的容器,磁盘空间不变呢

kubernetes基础之docker的目录是如何“拼”起来的---联合文件系统(UnionFS)_nginx_04

结束

这就是docker的联合文件系统(UnionFS),你懂了嘛
最后送大家一个福利,为什么我没说那个*****-init目录,大家可以看看
[root@localhost ~]# ls /var/lib/docker/overlay2/2c609b1870a4e9da2c2cdb2f411827250aeebb42a41d165e21caeb1864a10459/diff
b  bb  bbb  bbb.txt  bb.txt  b.txt  etc  root  run  var
[root@localhost ~]# ls  /var/lib/docker/overlay2/2c609b1870a4e9da2c2cdb2f411827250aeebb42a41d165e21caeb1864a10459-init/diff
dev  etc
[root@localhost ~]# ls  /var/lib/docker/overlay2/2c609b1870a4e9da2c2cdb2f411827250aeebb42a41d165e21caeb1864a10459-init/diff/dev/
console  pts  shm
[root@localhost ~]# ls  /var/lib/docker/overlay2/2c609b1870a4e9da2c2cdb2f411827250aeebb42a41d165e21caeb1864a10459-init/diff/etc
hostname  hosts  mtab  resolv.conf     
## 大家可以看看容器里面的hostname hosts resolv.conf与宿主机的这几个文件有什么区别,开发下同志们的大脑