当前位置:网站首页>The caching feature of docker image and dockerfile
The caching feature of docker image and dockerfile
2022-07-04 23:14:00 【Zuo Mingshui】
One 、Docker The caching characteristics of the image
1、 Build a mirror image
Docker It will cache the image layer of the existing image , When building a new image , If a mirror layer already exists , Just use it , No need to recreate .
for example :
In front of Dockerfile Add a little new content to , Copy a file into the image imageFuGai:
vi Dockerfile
# This my first CentOS Dockerfile
# Version 1.0
# Base images
FROM centos
#MAINTAINER
MAINTAINER zola
#ENV
#ADD
#RUN
RUN yum install -y wget
COPY imageFuGai
#WORKDIR
#EXPOSE
#CMD
[[email protected] ~]# pwd
/root
[[email protected] ~]# ll
total 8
-rw-------. 1 root root 1616 Apr 29 17:42 anaconda-ks.cfg
-rw-r--r--. 1 root root 189 May 10 20:09 Dockerfile
-rw-r--r--. 1 root root 0 May 10 20:02 imageFuGai
[[email protected] ~]# docker build -t centos-with-wget-02 .
Sending build context to Docker daemon 17.41kB
Step 1/4 : FROM centos
---> 470671670cac
Step 2/4 : MAINTAINER zola
---> Using cache
---> fe6ec852b0c9
Step 3/4 : RUN yum install -y wget
---> Using cache
---> 1cca79f8389d
Step 4/4 : COPY imageFuGai /
---> e0ebf5dc3b3c
Successfully built e0ebf5dc3b3c
Successfully tagged centos-with-wget-02:latest
[[email protected] ~]#
explain :
Step 2 and Step 3: The same has been run before RUN Instructions , This time, use the image layer in the cache directly fe6ec852b0c9 and 1cca79f8389d.
Step 4: perform COPY Instructions .
The process is to start the temporary container , Copy imageFuGai, Commit a new mirror layer e0ebf5dc3b3c, Delete temporary container .
Do not use cache when building images , Can be in docker build Add... To the order --no-cache Parameters
docker build -t --no-cache centos-with-wget-02 .
Dockerfile Each instruction in creates a mirror layer , The upper layer depends on the lower layer . whenever , As long as one layer changes , The cache of all layers above it will be invalidated .
If the change Dockerfile The order in which instructions are executed , Or modify or add instructions , Will invalidate the cache .
2、 Download mirroring
Docker Cache will also be used when downloading images . For example, we download httpd Mirror image
docker history debian
Two 、 debugging Dockerfile
1、 adopt Dockerfile The process of building a mirror
from base Mirror runs a container
Execute an order , Modify the container
Execution is similar to docker commit The operation of , Create a new mirror layer
Docker Then run a new container based on the image just submitted
repeat 2-4 Step , until Dockerfile All instructions in are executed
2、Dockerfile For some reason, the execution of an instruction fails , We will also be able to get the image constructed by the successful execution of the previous instruction , This is for debugging Dockerfile Very helpful
If Dockerfile In the third step RUN Instructions 1 When the failure . We can use the image created in step 2 111133223 debug , The way is through docker run -it Start a container for the image
docker run -it 111133223
By hand RUN Instructions 1 It's easy to locate the cause of failure
3、 ... and 、Dockerfile Commonly used instructions
Reference link :
https://www.runoob.com/docker/docker-dockerfile.html
FROM
Appoint base Mirror image .
MAINTAINER
Set the author of the mirror , It could be any string .
COPY
The file from build context Copy to mirror .
COPY Support two forms :
COPY src dest
COPY ["src", "dest"]
Be careful :src Only... Can be specified build context Files or directories in .
ADD
And COPY similar , from build context Copy the file to the image . The difference is , If src It's an archive (tar, zip, tgz, xz etc. ), The file will be automatically unzipped to dest.
ENV
Set the environment variable , Environment variables can be used by subsequent instructions . for example :
...
ENV MY_VERSION 1.3
RUN yum -y install -y mypackage=$MY_VERSION
...
EXPOSE
Specifies that the process in the container will listen on a port ,Docker You can expose the port . We will discuss in detail in the container network section .
VOLUME
Declare a file or directory as volume. We will discuss in detail in the container storage section .
WORKDIR
For the back of the RUN, CMD, ENTRYPOINT, ADD or COPY Directive sets the current working directory in the image .
RUN
Runs the specified command in the container .
CMD
Run the specified command when the container starts .
Dockerfile There can be more than one CMD Instructions , But only the last one works .CMD Can be docker run After the parameter replacement .
ENTRYPOINT
Sets the command to run when the container starts .
Dockerfile There can be more than one ENTRYPOINT Instructions , But only the last one works .CMD or docker run Subsequent parameters are passed as parameters to the ENTRYPOINT.
notes :Dockerfile Support with “#” Opening comment
Be careful :Dockerfile Every time an instruction is executed, it will be in docker Build a new floor on it . So there are too many meaningless layers , It will cause the mirror image to expand too much . for example :
FROM centos
RUN yum install wget
RUN wget -O redis.tar.gz "http://download.redis.io/releases/redis-5.0.3.tar.gz"
RUN tar -xvf redis.tar.gz
The above execution will create 3 Layer mirror . It can be simplified to the following format :
FROM centos
RUN yum install wget \
&& wget -O redis.tar.gz "http://download.redis.io/releases/redis-5.0.3.tar.gz" \
&& tar -xvf redis.tar.gz
With && Symbolic link command , After this , Only create 1 Layer mirror
RUN、CMD、ENTRYPOINT It's important and confusing
Four 、RUN、CMD and ENTRYPOINT
RUN Execute the command and create a new mirror layer ,RUN Often used to install software packages
CMD Set the default command and its parameters after the container is started , but CMD It can be docker run Replace with the command line argument
ENTRYPOINT Configure the commands to run when the container starts
1、Shell and Exec Format
There are two ways to specify RUN、CMD and ENTRYPOINT Commands to run :Shell Format and Exec Format , There are subtle differences in the use of the two .
Shell Format
<instruction> <command>
Exec Format
<instruction> ["executable", "param1", "param2", ...]
CMD and ENTRYPOINT Recommended Exec Format , Because the instructions are more readable , Easier to understand .RUN Two formats shell Format and Exec Format is OK
2、RUN There are two formats
Shell Format :RUN
Exec Format :RUN ["executable", "param1", "param2"]
Use RUN Install multiple packages :
RUN apt-get update && apt-get install -y \
bzr \
cvs \
git \
mercurial \
subversion
Be careful :apt-get update and apt-get install Put in a RUN To execute in an instruction , This ensures that the latest package is installed every time . If apt-get install In a separate RUN In the implementation of , Will use apt-get update Create a mirror layer , This layer may have been cached a long time ago .
3、 Best practices
Use RUN Instructions to install applications and software packages , Build a mirror image .
If Docker The purpose of the image is to run applications or services , Like running a MySQL, Should be used first Exec Format ENTRYPOINT Instructions .CMD for ENTRYPOINT Provide additional default parameters , Simultaneously available docker run The command line replaces the default parameters .
If you want to set the default startup command for the container , You can use CMD Instructions . The user can be in docker run Replace this default command on the command line .
Wechat link :https://mp.weixin.qq.com/s/ZA7NtaCfgOhYCcj-k5tQDA
For details, please see , official account
边栏推荐
- Basic knowledge of database
- A mining of edu certificate station
- 【二叉树】节点与其祖先之间的最大差值
- HMS core unified scanning service
- Redis入门完整教程:GEO
- Photoshop batch adds different numbers to different pictures
- Redis入门完整教程:键管理
- Google Earth engine (GEE) - globfire daily fire data set based on mcd64a1
- 微信公众号解决从自定义菜单进入的缓存问题
- [graph theory] topological sorting
猜你喜欢
LabVIEW中比较两个VI
Redis入門完整教程:Pipeline
Complete tutorial for getting started with redis: bitmaps
Redis introduction complete tutorial: slow query analysis
Redis入门完整教程:客户端通信协议
智力考验看成语猜古诗句微信小程序源码
D3.js+Three. JS data visualization 3D Earth JS special effect
EditPlus--用法--快捷键/配置/背景色/字体大小
【js】-【排序-相关】-笔记
Excel shortcut keys - always add
随机推荐
[Jianzhi offer] 6-10 questions
Google collab trample pit
Redis introduction complete tutorial: Collection details
Redis入門完整教程:Pipeline
CTF竞赛题解之stm32逆向入门
微信小程序显示样式知识点总结
A complete tutorial for getting started with redis: hyperloglog
[machine learning] handwritten digit recognition
推荐收藏:跨云数据仓库(data warehouse)环境搭建,这货特别干!
Docker镜像的缓存特性和Dockerfile
Redis入门完整教程:键管理
图片懒加载的原理
Redis:Redis的事务
Redis入门完整教程:初识Redis
Redis入门完整教程:有序集合详解
Redis:Redis消息的发布与订阅(了解)
MariaDB的Galera集群应用场景--数据库多主多活
Redis: redis transactions
Qt个人学习总结
How can enterprises cross the digital divide? In cloud native 2.0