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

How to understand the hierarchical structure of docker Mirror

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

Share

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

In this issue, the editor will bring you about how to understand the hierarchical structure of docker images. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

Docker supports the creation of new images by extending existing images.

In fact, 99% of the images in Docker Hub are built by installing and configuring the required software in the base image. For example, we are now building a new image. The Dockerfile is as follows:

The new ① image no longer starts with scratch, but is built directly on the Debian base image.

② installs the emacs editor.

③ installs apache2.

Run bash when the ④ container starts.

As you can see, the new image is generated by overlaying the base image layer by layer. Each time a software is installed, an additional layer is added to the existing image.

Why should Docker images adopt this hierarchical structure?

One of the biggest benefits is-sharing resources.

For example, if multiple images are built from the same base image, Docker Host only needs to save one base image on disk, and only one base image needs to be loaded in memory to serve all containers. And each layer of the mirror can be shared, which we will discuss in more depth later.

At this point, someone may ask: if multiple containers share a basic image, when a container modifies the contents of the basic image, such as the file under / etc, will the / etc of other containers be modified as well?

The answer is no!

Modifications are limited to a single container.

This is the container Copy-on-Write feature that we will learn next.

Writable container layer

When the container starts, a new writable layer is loaded on top of the mirror.

This layer is often called the "container layer", and those below the "container layer" are called the "mirror layer".

All changes to the container-whether adding, deleting, or modifying files-occur only in the container layer.

Only the container layer is writable, and all mirror layers below the container layer are read-only.

Let's delve into the details of the container layer.

The number of mirror layers may be large, and all mirror layers will be combined to form a unified file system. If there is a file with the same path in different layers, such as / a, the upper / a will overwrite the lower / a, that is, the user can only access the upper file / a. In the container layer, what the user sees is a superimposed file system.

Add Fil

When you create a file in a container, the new file is added to the container layer.

Read File when you read a file in a container, Docker looks for the file in each mirror layer from top to bottom. Once found, copy it to the container layer, then open it and read it into memory.

Modify File when you modify an existing file in the container, Docker looks for the file in each mirror layer from top to bottom. Once found, copy it to the container layer and modify it.

Delete File when you delete a file in the container, Docker also looks for the file in the mirror layer from top to bottom. When found, the delete operation is recorded in the container layer.

A copy of the data is copied only when it needs to be modified, a feature called Copy-on-Write. It can be seen that the container layer saves the changed part of the image and does not make any changes to the image itself.

This explains the problem we raised earlier: the container layer records the changes to the image, and all the image layers are read-only and will not be modified by the container, so the image can be shared by multiple containers.

The above is how to understand the hierarchical structure of docker images shared by the editor. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

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