In order to package up your own Docker image, you write a text document called a Dockerfile that lists all the steps needed to both make the image and use the Docker command line to build it。
例一

例二
-
新建dockerfile
图片.png
提示,最好在FROM指令中指出基础镜像的tag,否则当你下次重新build时,你的镜像会经常自动更新,更有甚者,会导致编译不通过。

-
构建镜像
图片.png
图片.png
说明:
The image build command gets executed by the Docker server—the client simply sends the details through. That’s why you must specify a path (using . for the current working directory in this case). The client sends the contents of the path to the server, and the server stores it in a working folder it uses to build the image. That folder is called the build context。
-
查看镜像(存储于Docker image cache)
图片.png - Dockerfile里面的指令
图片.png
4.1 例子
Dockerfiler文件:
图片.png
制作镜像:
图片.png
创建容器 并用nc连接后发消息:
图片.png
我们可以看看产生了什么(使用 docker container exec 在容器中执行一个命令,并输出到host上):
root@10049605-ThinkPad-T470-W10DG:/home/yay# docker container exec echo-server cat /server-logs/echo.out

我们可以查看这个挂载点/server-logs在主机上的位置:
root@10049605-ThinkPad-T470-W10DG:/home/yay# docker container inspect -f {{.Mounts}} echo-server

我们看看我们host主机上被挂载的文件内容
--依据上面的输出,切换到对应的目录:
root@10049605-ThinkPad-T470-W10DG:/home/yay# cd /var/lib/docker/volumes/09167cfdc5d561b0dcfaef7c73301c7a6ae10ca0cdb8eb7c648c048092fab506/_data
--省略输出内容...
root@10049605-ThinkPad-T470-W10DG:/var/lib/docker/volumes/09167cfdc5d561b0dcfaef7c73301c7a6ae10ca0cdb8eb7c648c048092fab506/_data# cat echo.out

5 Docker镜像是如何构建出来的
看例子:
5.1 创建两个dockfile
内容如下:


5.3 可以查看镜像中的所有layers

5.4 接下来构建另外一个镜像

然后查看所有layers,并和a的layers作对比:

-
镜像删除(自己猜测后实验)
image.png - 总结
你只应当对那些需要被重用的层作缓存, 有状态的层不要让它发生缓存现象








