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

What is the principle of mirror layering in Docker?

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article will explain in detail what is the principle of image layering in Docker. The content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

Base Mirror

Base image has two meanings:

Build from scratch without relying on other images

Other images can be extended on the basis of them

Therefore, base images are generally Docker images of various Linux distributions, such as Ubuntu,Debian or CentOS.

The base image is provided with the smallest installed Linux distribution.

Most of our images will be based on base images. Therefore, an officially released base image is usually used. You can find it in docker hub. Like centos: https://hub.docker.com/_/centos.

We can build the docker base image ourselves, or we can use the existing base image directly. Like centos. We can pull it directly from docker hub.

Pull

Docker pull centos

View

Docker images centos REPOSITORY TAG IMAGE ID CREATED SIZEcentos latest 1e1148e4cc2c 2 months ago 202MB

You can see that only 200mb is the latest centos image, isn't it too small? This is because the docker image uses the kernel of the docker host machine directly at run time.

The Linux operating system consists of user space and kernel space.

The kernel space is kernel, the user space is rootfs, and the main difference between different distributions is rootfs. For example, Ubuntu 14.04 uses upstart management services and apt management packages, while CentOS 7 uses systemd and yum. These are differences in user space, and there is little difference in Kernel.

Therefore, Docker can support multiple Linux images at the same time, simulating different operating system environments.

The base image is just that the user space is consistent with the distribution, and the kernel space uses the Kernel of the Docker host machine.

Storage structure

The above shows how to download a base image. We usually build our own images based on this base image. For example, add a nginx load balancer to centos. First, you need to know what the structure of the mirror is.

Official document: https://docs.docker.com/storage/storagedriver/

Hierarchical structure of Docker Mirror

When you start the mirror, a new writeable layer is loaded into the top layer of the mirror. This layer is often called the "container layer", and below it is the "mirror layer".

The container layer can be read and written, and all file changes and writes to the container occur at this layer. The mirror layer only allows reading, read-only.

Replication policy on modification (copy-on-write)

Docker uses a replication policy on modification to ensure the security of base images, as well as higher performance and space utilization.

When the container needs to read the file

Start looking down from the top mirror layer, find it and read it into memory. If it is already in memory, you can use it directly. In other words, Docker containers running on the same machine share the same files at run time.

When the container needs to modify the file

Search from top to bottom, find it and copy it to the container layer. For the container, you can see the file in the container layer, but you can't see the file in the mirror layer, and then modify the file in the container layer directly.

When the container needs to delete the file

Search from top to bottom, and record deletion in the container after finding it. It is not a real deletion, but a soft deletion. As a result, the mirror volume will only increase, not decrease.

When the container needs to add files

Directly in the top layer of the container writable layer increases, will not affect the mirror layer.

Simplification and Optimization of Mirror

Optimize basic mirroring

When choosing the basic image, choose the appropriate smaller image. The commonly used Linux system images are generally Ubuntu, CentOs, Alpine, etc.

Tandem Dockerfile instruction

In Dockerfile, each instruction creates a mirror layer, increasing the size of the mirror. Changes to the current layer do not affect the previous layer.

Use & & concatenated instructions (in RUN instructions)

Remember clean after installing the software

Specific examples are as follows:

Custom Dockerfile:

FROM ubuntu:14.04# basic source image MAINTAINER xiongkun# describes the creator of the image, name and mailbox WORKDIR / homeRUN dd if=/dev/zero of=50M.file bs=1M count=50# to create a 50m test file RUN rm-rf 50M.file# to delete the file

Optimized Dockerfile:

The FROM ubuntu:14.04# basic source image MAINTAINER xiongkun# describes the creator of the image, the name and mailbox WORKDIR / homeRUN dd if=/dev/zero of=50M.file bs=1M count=50 & & rm-rf 50M.file# to create the file, and deletes the file in this layer. This is the end of sharing about the principle of image layering in Docker. I hope the above content can be helpful to you and learn more. If you think the article is good, you can share it for more people to see.

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