当前位置:网站首页>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
边栏推荐
- 数据库基础知识
- SHP data making 3dfiles white film
- 可观测|时序数据降采样在Prometheus实践复盘
- 微信小程序显示样式知识点总结
- Principle of lazy loading of pictures
- 壁仞科技研究院前沿技术文章精选
- Sword finger offer 68 - ii The nearest common ancestor of binary tree
- Excel shortcut keys - always add
- Insert sort, select sort, bubble sort
- CTF competition problem solution STM32 reverse introduction
猜你喜欢
The difference between cout/cerr/clog
位运算符讲解
【二叉树】节点与其祖先之间的最大差值
Complete tutorial for getting started with redis: bitmaps
Redis入门完整教程:哈希说明
C语言快速解决反转链表
Redis入门完整教程:慢查询分析
A complete tutorial for getting started with redis: redis shell
The difference between debug and release
JS 3D explosive fragment image switching JS special effect
随机推荐
Application of machine learning in housing price prediction
D3.js+Three. JS data visualization 3D Earth JS special effect
Redis introduction complete tutorial: detailed explanation of ordered collection
A complete tutorial for getting started with redis: understanding and using APIs
Set up a website with a sense of ceremony, and post it to 1/2 of the public network through the intranet
Async await used in map
Notepad++--编辑的技巧
Redis introduction complete tutorial: Collection details
P2181 diagonal and p1030 [noip2001 popularization group] arrange in order
Redis入门完整教程:事务与Lua
Redis入门完整教程:Redis Shell
S32 Design Studio for ARM 2.2 快速入门
PICT 生成正交测试用例教程
Redis入门完整教程:GEO
[Jianzhi offer] 6-10 questions
VIM editor knowledge summary
Photoshop batch adds different numbers to different pictures
Redis入门完整教程:API的理解和使用
Servlet服务器端和客户端中文输出乱码问题
云服务器设置ssh密钥登录