- 参考
笔记 – Docker分层存储演示
- 下载nginx:latest
$ docker image pull nginx:latest $ docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest e81eb098537d 8 days ago 109MB
- 看到下载下来的最新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
- Line 3: 第一步执行Dockerfile1 中的第一行,因为此时并没有对nginx:latest镜像做任何改动,对比镜像ID,发现用的就是下载下来的
e81eb098537d
(第四行) - Line 5: 第二步执行Dockerfile1中的第二行,因为复制了文件,所以创建新的镜像
5fb9895b2c98
(第7行)
- Line 3: 第一步执行Dockerfile1 中的第一行,因为此时并没有对nginx:latest镜像做任何改动,对比镜像ID,发现用的就是下载下来的
- 用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
- Line 3: 第一步执行Dockerfile2 中的第一行,仍然使用镜像
e81eb098537d
。 - Line 5: 第二步执行Dockerfile1中的第二行,创建镜像
d353c0c66814
。
- Line 3: 第一步执行Dockerfile2 中的第一行,仍然使用镜像
- 对比镜像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 ……
- 看到nginx-with-html1和nginx-with-html2共用了镜像
e81eb098537d
,也就是官方的nginx:latest。在这一层之上,各自存储了自己复制的不同内容(index1.html与index2.html)。Docker用这种方式避免了反复存储相同镜像造成的浪费并提高了PUSH/PULL的效率,这就是分层存储,可以用下面的图表示它们分层存储的关系:- 镜像nginx-with-html1包含了
5fb9895b2c98
+e81eb098537d
+… - 镜像nginx-with-html2包含了
d353c0c66814
+e81eb098537d
+…
- 镜像nginx-with-html1包含了
- 看到nginx-with-html1和nginx-with-html2共用了镜像
- 启动镜像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
- 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>
- 用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
- 容器本身是建立在镜像上的又一个可读写的层(例如可以用apt-get在ubuntu容器中安装新的app)。所以加上容器,分层存储可以表示成:
- 容器nginx1包括:
2d2f0313b01f
+5fb9895b2c98
+e81eb098537d
+… - 容器nginx2包括:
976c59956749
+d353c0c66814
+e81eb098537d
+… - 容器nginx3包括:
5767eb1d2a9e
+d353c0c66814
+e81eb098537d
+…
- 容器nginx1包括:
- 删除容器和镜像
$ docker container rm -f $(docker container ls -q) 5767eb1d2a9e 976c59956749 2d2f0313b01f $ docker image rm nginx-with-html1 $ docker image rm nginx-with-html2