Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Dockers image layering

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Dockers image layering

1 minimum mirror image of Dockers

[root@localhost ~] # docker pull hello-world// download the smallest image [root@localhost ~] # docker images// to view the image

[root@localhost ~] # docker run hello-world// run hello-world (inside is a text brief introduction to the operation of docker)

The composition of dockerfile

1) FROM:scratch (scratch, scratch)

2) COPY:hello /

3) CMD: ["/ hello"]

FROM

Syntax: FROM [:] explanation: to set the image to be created based on which image, the FROM instruction must be the first instruction of the entire Dockerfile. If the specified image does not exist, it will be automatically downloaded from the Docker Hub by default.

COPY

Syntax: COPY explanation: the usage is the same as ADD, but url is not supported, so use docker build-

< somefile时该指令不能使用。 CMD 语法:①CMD ["executable", "param1", "param2"] #将会调用exec执行,首选方式 ②CMD ["param1", "param2"] #当使用ENTRYPOINT指令时,为该指令传递默认参数 ③CMD [ | ] #将会调用/bin/sh -c执行 解释:CMD指令中指定的命令会在镜像运行时执行,在Dockerfile中只能存在一个,如果使用了多个CMD指令,则只有最后一个CMD指令有效。当出现ENTRYPOINT指令时,CMD中定义的内容会作为ENTRYPOINT指令的默认参数,也就是说可以使用CMD指令给ENTRYPOINT传递参数。 注意:RUN和CMD都是执行命令,他们的差异在于RUN中定义的命令会在执行docker build命令创建镜像时执行,而CMD中定义的命令会在执行docker run命令运行镜像时执行,另外使用第一种语法也就是调用exec执行时,命令必须为绝对路径。 2、Base镜像(基础镜像) Centos:7镜像的dockerfile FROM scratchADD centos-7-x86_ _64-docker.tar.xz /LABEL org. label-schema. schema-version="1.0" \|org. label-schema. name="Centos Base Image" \org. labe1-schema. vendor="centos" \org. labe1-schema. 1icense="GPLV2" \org. labe1-schema build-date="20190305 'CMD ["/bin/bash"] 3、镜像的分层 1)dockerfile的书写格式为:Dockerfile(首字母大写,包括文件名称) 2)From:构建镜像有两种方式,一种scratch(从零构建),另一种可以基于某个镜像开始构建 3)镜像所运行的操作(用户所期望的) [root@localhost ~]# mkdir test//创建测试目录[root@localhost ~]# cd test//进入测试目录[root@localhost ~]#vim Dockerfile//编写DockerfileFROM centos:7 RUN yum -y install vim #或["yum","install","vim"]RUN yum -y install net-toolsCMD ["/bin/bash"] 执行一下 [root@localhost test]# docker build -t centos7-vim-net-tools:12-11 .//使用当前目录的 Dockerfile 创建镜像,标签为 centos7-vim-net-tools:12-11build: 使用 Dockerfile 创建镜像-t:标签. :当前目录 执行的层次

Hierarchical summary of 4.Dockerfile images

The container is the cornerstone of the image, and the container is the instance after the image is run. When the image is run as a container, you only have read-only permission for all data in the image. If you need to modify or delete the image source file, it is done at the container layer (writable layer), and the COW (copy on write) replication mechanism is used.

Cache characteristics of Docker Mirror

1. Create a new Dockerfile file

[root@localhost ~] # vim DockerfileFROM centos:7RUN yum-y install vimRUN yum-y install net-toolsRUN yum-y install wgetCMD ["/ bin/bash"] [root@localhost ~] # docker build-t new-centos. / / create an image named new-centos using the Dockerfile of the current directory

If the same image is used in the same layer, you can use the cache instead of downloading it. (if the first floor is different, then the same below is useless and needs to be downloaded again)

3. Create a new Dockerfile again

[root@localhost ~] # mkdir test1 [root@localhost ~] # cd test [root@localhost test] # cd.. / test1 [root@localhost test1] # vim DockerfileFROM centos:7RUN yum-y install vimRUN yum-y install wgetRUN yum-y install net-toolsCMD ["/ bin/bash"] [root@localhost test1] # docker build-t centos-new. / / create an image named centos-new using the Dockerfile of the current directory

Even if the operation in the mirror layer is the same, it must be in the same layer to use the caching features of dockerfile

If you do not want to use caching during mirroring, you can use the-- no-cache option.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report