当前位置:网站首页>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
边栏推荐
- cout/cerr/clog的区别
- Docker镜像的缓存特性和Dockerfile
- Actual combat simulation │ JWT login authentication
- Redis入门完整教程:Redis使用场景
- Complete tutorial for getting started with redis: bitmaps
- 小程序vant tab组件解决文字过多显示不全的问题
- Redis: redis transactions
- CTF competition problem solution STM32 reverse introduction
- HMS core unified scanning service
- 【ODX Studio编辑PDX】-0.2-如何对比Compare两个PDX/ODX文件
猜你喜欢
S32 Design Studio for ARM 2.2 快速入门
Google Earth engine (GEE) - globfire daily fire data set based on mcd64a1
colResizable. JS auto adjust table width plug-in
P2181 对角线和P1030 [NOIP2001 普及组] 求先序排列
Excel 快捷键-随时补充
Complete tutorial for getting started with redis: bitmaps
A mining of edu certificate station
Redis入门完整教程:哈希说明
Redis入门完整教程:GEO
Redis: redis message publishing and subscription (understand)
随机推荐
Complete tutorial for getting started with redis: bitmaps
Google Earth engine (GEE) - tasks upgrade enables run all to download all images in task types with one click
Redis introduction complete tutorial: detailed explanation of ordered collection
Explanation of bitwise operators
SHP data making 3dfiles white film
Sword finger offer 67 Convert a string to an integer
Object detection based on OpenCV haarcascades
Notepad++--编辑的技巧
Redis入门完整教程:慢查询分析
Header file duplicate definition problem solving "c1014 error“
Stm32 Reverse Introduction to CTF Competition Interpretation
Duplicate ADMAS part name
Ffmpeg quick clip
Basic knowledge of database
Set up a website with a sense of ceremony, and post it to 1/2 of the public network through the intranet
实战模拟│JWT 登录认证
QT drawing network topology diagram (connecting database, recursive function, infinite drawing, dragging nodes)
The difference between cout/cerr/clog
HMS core unified scanning service
Sword finger offer 68 - ii The nearest common ancestor of binary tree