[docker] 7. Image loading principle, layering principle and commit image
Bite the apple and cry 2021-06-04 10:48:09

One 、 What is mirror image

Image is a lightweight 、 Executable standalone package , It is used to package software running environment and software developed based on running environment .
It contains everything you need to run a piece of software , Including code 、 Runtime environment 、 library 、 Environment variables and profiles .
All applications , Pack it directly into docker Mirror image , You can just run .

How to get a mirror image :

  • Download from remote repository
  • From other places copy
  • Make your own mirror image DockerFile

Two 、docker Image loading principle

1. Federated file system UnionFS

UnionFS It's a kind of layering 、 Lightweight and high performance file system . Support file system modification as a submission to layer by layer , At the same time, you can put different directories
Mount to the same virtual file system . This is when we download the image , You can see this effect .

For example, there are the same documents involved , Then you can share it , Save a lot of resources .

UnionFS yes docker The foundation of the mirror , Images can be inherited by layering , Based on the basic image ( No father image ), Sure
Make a variety of specific application images .

characteristic : Loading multiple file systems at once , But from the outside it looks like , Only one file system can be seen , Joint loading will stack up all layers of file systems , So the final file system will
Contains all the underlying files and directories .

2. Image loading principle

docker The image of is actually composed of file systems layer by layer , This kind of hierarchical file system is the above UnionFS. next , Internally, it is divided into 2 part :

  • bootfs(boot file system):docker The bottom of the image is bootfs, It mainly includes bootloader( loader ) and kernel( kernel ).
    bootloader Mainly boot loading kernel,linux It will load when it is just started bootfs file system . This layer is similar to the typical linux/Unix The system is the same , contain bootloader and kernel.
    When boot After loading , The whole kernel is in memory , At this time, the right to use memory has been granted by bootfs To the kernel , At this time, the system will also uninstall bootfs.
    The load here , It can be understood as , We windows When the computer is on , From black screen to operating system .

  • rootfs(root file system): stay bootfs above , It contains the typical linux In the system /dev、/proc、/bin、/etc Etc. standard directories and documents .
    rootfs Is a variety of different operating system distributions , such as Ubuntu、Centos wait .

As shown in the figure :

In the picture debian System as an example , From left to right , It is divided into 3 A process :

  • chart 1, The state of beginning , Downloaded a debian System .
  • chart 2, Installed a emacs, Now you can see in the picture 1 On the basis of , Add a layer Image.
  • chart 3, There's another one Apache, At this point in the figure 2 On the basis of that, I added a layer of Image.

Illustrates the docker The image of is actually made up of layer by layer file system . For different linux Release version ,bootfs Basically consistent ,rootfs There will be differences , So different
Distributions can share bootfs.

in addition , stay docker The operating system on is usually a compact version , stay VM Install a centos Image size 1 individual G many , And in the docker Upper centos Mirror only 200M size .
Because the bottom layer directly uses the kernel of the host , I just need to provide rootfs That's it , therefore rootfs It can be very small , Just include the most basic commands 、 Tools and libraries .
thus , It's starting up fast too , Because the most time-consuming boot loading process is gone .

3、 ... and 、 Layering principle

I know the loading principle of image , Let's look back at the principle of image layering . I mentioned before , When the image is downloaded, it is downloaded hierarchically , Some layers, if they already exist , You don't have to download it again .
For example, I download one redis Mirror image .

The biggest advantage of this way is resource sharing . For example, there are multiple images from the same BASE It's a mirror image , Then the host only needs to keep it on the disk 1 branch BASE Mirror image , At the same time, in memory
You only need to load one BASE Mirror image , So that all the containers can be used . in addition , Every layer of the image can be shared .

Can pass docker image inspect To see the layering of the image , For example, check the download just now redis Mirror image :

docker image inspect redis:latest

be-all docker Mirrors all start with a basic mirror layer , When making changes or adding new content , It will be above the current mirror layer , Create a new mirror layer .
such as :

I'm going to make a mirror now .

  1. This image is based on Ubuntu linux 16.04, This is also the first layer of the mirror image .
  2. Continue to install python package , A second mirror layer will be created on top of the first layer .
  3. If you keep patching , A third mirror layer will be created .

want Be careful Yes. :
While adding extra mirror layers , The image always remains the current combination of all the mirrors , Here's the picture :

Here, each image layer contains 3 File , And the mirror image contains the information from 2 It's a mirror layer 6 File .

Now? , If in the second layer file 5 Upgrade version required . At this time, the files in the upper image will cover the corresponding files in the lower image , The updated version of the file is added to the image as a new image layer .

docker Through the way of storage engine to realize the image layer stack , And ensure that the multi mirror layer is displayed as a unified file system .

Four 、commit Mirror image

Through the above understanding , Now we know how the mirror works , So we can make a mirror ourselves .

such as , Now? pull One tomcat Mirror as the base layer , When I start this image , I made my own changes in the container , I think my changes are very good , The image is better . So I need
To save the state of this container , adopt commit command , Submit image .

docker commit -m=" Submit description " -a=" author " Containers id Target image name : Version label

function tomcat after , Enter into webapps Next , It's found that there is no project , Because it's a castrated version .

Now I put webapps.dist All of the following copy To webapps Next .

Now I use ip:8080 You can access the project .

Now I submit this modified container .

docker commit -m="pingguo first commit image" -a="pingguo" 03844ff66434 tomcatpingguo:1.0

After successful submission ,docker images Look at the mirror image , It's found that it has been saved locally .

Through their own submit image operation , Come back and experience the layering of images , I wonder if I have a deeper understanding ?

Please bring the original link to reprint ,thank
Similar articles