当前位置:网站首页>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

边栏推荐
- Wechat official account solves the cache problem of entering from the customized menu
- Set up a website with a sense of ceremony, and post it to 1/2 of the public network through the intranet
- Advantages of Alibaba cloud international CDN
- Google Earth engine (GEE) - tasks upgrade enables run all to download all images in task types with one click
- Question brushing guide public
- heatmap. JS picture hotspot heat map plug-in
- MySQL数据库备份与恢复--mysqldump命令
- 浅聊一下中间件
- The difference between debug and release
- cout/cerr/clog的区别
猜你喜欢
随机推荐
Stm32 Reverse Introduction to CTF Competition Interpretation
One of the commonly used technical indicators, reading boll Bollinger line indicators
可观测|时序数据降采样在Prometheus实践复盘
OSEK标准ISO_17356汇总介绍
Redis入门完整教程:事务与Lua
Redis入门完整教程:键管理
Basic knowledge of database
CTF competition problem solution STM32 reverse introduction
SPH中的粒子初始排列问题(两张图解决)
【ODX Studio編輯PDX】-0.2-如何對比Compare兩個PDX/ODX文件
微信公众号解决从自定义菜单进入的缓存问题
The solution to the lack of pcntl extension under MAMP, fatal error: call to undefined function pcntl_ signal()
[roommate learned to use Bi report data processing in the time of King glory in one game]
【机器学习】手写数字识别
初试为锐捷交换机跨设备型号升级版本(以RG-S2952G-E为例)
【二叉树】节点与其祖先之间的最大差值
Wechat official account solves the cache problem of entering from the customized menu
Photoshop批量给不同的图片添加不同的编号
智力考验看成语猜古诗句微信小程序源码
Duplicate ADMAS part name









