笔记 -- Docker分层存储演示

笔记 – Docker分层存储演示

  • 下载nginx:latest
    $ docker image pull nginx:latest
    $ docker image ls
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    nginx               latest              e81eb098537d        8 days ago          109MB
    
    1. 看到下载下来的最新nginx镜像ID是e81eb098537d
  • 下载示例要用的文件到当前文件夹下
    $ wget https://raw.githubusercontent.com/MingxuanHu/learn-docker/master/dockerfile/nginx/Dockerfile1
    $ wget https://raw.githubusercontent.com/MingxuanHu/learn-docker/master/dockerfile/nginx/Dockerfile2
    $ wget https://raw.githubusercontent.com/MingxuanHu/learn-docker/master/dockerfile/nginx/index1.html
    $ wget https://raw.githubusercontent.com/MingxuanHu/learn-docker/master/dockerfile/nginx/index2.html
    
  • 用Dockerfile1创建镜像
    $ docker image build -f ./Dockerfile1 -t nginx-with-html1 ./
    Sending build context to Docker daemon   5.12kB
    Step 1/2 : FROM nginx:latest
     ---> e81eb098537d
    Step 2/2 : COPY ./index1.html /usr/share/nginx/html/index.html
     ---> Using cache
     ---> 5fb9895b2c98
    Successfully built 5fb9895b2c98
    Successfully tagged nginx-with-html1:latest
    
    1. Line 3: 第一步执行Dockerfile1 中的第一行,因为此时并没有对nginx:latest镜像做任何改动,对比镜像ID,发现用的就是下载下来的e81eb098537d (第四行)
    2. Line 5: 第二步执行Dockerfile1中的第二行,因为复制了文件,所以创建新的镜像5fb9895b2c98 (第7行)
  • 用Dockerfile2创建镜像
    $ docker image build -f ./Dockerfile2 -t nginx-with-html2 ./
    Sending build context to Docker daemon   5.12kB
    Step 1/2 : FROM nginx:latest
     ---> e81eb098537d
    Step 2/2 : COPY ./index2.html /usr/share/nginx/html/index.html
     ---> Using cache
     ---> d353c0c66814
    Successfully built d353c0c66814
    Successfully tagged nginx-with-html2:latest
    
    1. Line 3: 第一步执行Dockerfile2 中的第一行,仍然使用镜像e81eb098537d
    2. Line 5: 第二步执行Dockerfile1中的第二行,创建镜像d353c0c66814
  • 对比镜像nginx-with-html1和nginx-with-html2的历史
    $ docker image history nginx-with-html1
    IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
    5fb9895b2c98        10 minutes ago      /bin/sh -c #(nop) COPY file:5b1fc47f9bcc1178…   246B                
    e81eb098537d        8 days ago          /bin/sh -c #(nop)  CMD ["nginx" "-g" "daemon…   0B                  
    ……
    $ docker image history nginx-with-html2
    IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
    d353c0c66814        10 minutes ago      /bin/sh -c #(nop) COPY file:6370719431bb1728…   246B                
    e81eb098537d        8 days ago          /bin/sh -c #(nop)  CMD ["nginx" "-g" "daemon…   0B                  
    ……    
    
    1. 看到nginx-with-html1和nginx-with-html2共用了镜像e81eb098537d,也就是官方的nginx:latest。在这一层之上,各自存储了自己复制的不同内容(index1.html与index2.html)。Docker用这种方式避免了反复存储相同镜像造成的浪费并提高了PUSH/PULL的效率,这就是分层存储,可以用下面的图表示它们分层存储的关系:
      other-shared-layers
      e81eb098537d
      5fb9895b2c98
      d353c0c66814
      1. 镜像nginx-with-html1包含了5fb9895b2c98 +e81eb098537d+…
      2. 镜像nginx-with-html2包含了d353c0c66814+e81eb098537d+…
  • 启动镜像nginx-with-html1和nginx-with-html2
    $ docker container run -d --name nginx1 -p 81:80 --rm nginx-with-html1
    $ docker container run -d --name nginx2 -p 82:80 --rm nginx-with-html2
    $ docker container run -d --name nginx3 -p 83:80 --rm nginx-with-html2
    
    1. nginx1是镜像nginx-with-html1的容器,nginx2和nginx3是镜像nginx-with-html2的容器。
    $ curl localhost:81
    <!doctype html>
    <html lang="en">
    <head>
      <meta charset="utf-8">
     
      <title>Your 1st Dockerfile worked!</title>
     
    </head>
     
    <body>
      <h1>You just successfully ran a container with index1.html copied into the image at build time!</h1>
    </body>
    </html>
    $ curl localhost:82
    <!doctype html>
    <html lang="en">
    <head>
      <meta charset="utf-8">
     
      <title>Your 2nd Dockerfile worked!</title>
     
    </head>
     
    <body>
      <h1>You just successfully ran a container with index2.html copied into the image at build time!</h1>
    </body>
    </html>
    $ curl localhost:83
    <!doctype html>
    <html lang="en">
    <head>
      <meta charset="utf-8">
     
      <title>Your 2nd Dockerfile worked!</title>
     
    </head>
     
    <body>
      <h1>You just successfully ran a container with index2.html copied into the image at build time!</h1>
    </body>
    </html>
    
    1. 用culr显示这三个容器可以正常运行,并且装载无误。
    $ docker container ls
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
    5767eb1d2a9e        nginx-with-html2    "nginx -g 'daemon of…"   52 seconds ago      Up 51 seconds       0.0.0.0:83->80/tcp   nginx3
    976c59956749        nginx-with-html2    "nginx -g 'daemon of…"   12 minutes ago      Up 12 minutes       0.0.0.0:82->80/tcp   nginx2
    2d2f0313b01f        nginx-with-html1    "nginx -g 'daemon of…"   12 minutes ago      Up 12 minutes       0.0.0.0:81->80/tcp   nginx1
    
    1. 容器本身是建立在镜像上的又一个可读写的层(例如可以用apt-get在ubuntu容器中安装新的app)。所以加上容器,分层存储可以表示成:
      other-shared-layers
      e81eb098537d
      5fb9895b2c98
      d353c0c66814
      2d2f0313b01f
      976c59956749
      5767eb1d2a9e
      1. 容器nginx1包括:2d2f0313b01f +5fb9895b2c98 +e81eb098537d+…
      2. 容器nginx2包括:976c59956749 +d353c0c66814+e81eb098537d+…
      3. 容器nginx3包括:5767eb1d2a9e +d353c0c66814+e81eb098537d+…
  • 删除容器和镜像
    $ docker container rm -f $(docker container ls -q)
    5767eb1d2a9e
    976c59956749
    2d2f0313b01f
    $ docker image rm nginx-with-html1
    $ docker image rm nginx-with-html2
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值