What is a mirror image

An image is a lightweight, executable, standalone package used to package a software runtime environment and software developed based on the runtime environment. It contains everything you need to run a piece of software, including code, runtime environment, libraries, environment variables, and configuration files. All apps, packaged directly as Docker images, can run directly.

How to get a mirror image:

  • Download from remote repository
  • Copy from somewhere else
  • Make your own image DockerFile

Docker image loading principle

1. UnionFS file system

UnionFS is a layered, lightweight, and high-performance file system. File system changes can be submitted one layer at a time, and different directories can be mounted to the same virtual file system. This is what we see when we download the image.



For example, if the same files are involved, they can be shared, greatly saving resources.

UnionFS is the basis of Docker images. The images can be inherited through layers. Based on the basic images (no parent images), various specific application images can be made.

Feature: Multiple file systems are loaded simultaneously, but only one file system can be seen from the outside. Joint loading adds all layers of file systems together, so that the final file system contains all the underlying files and directories.

2. Principle of image loading

Docker’s image actually consists of a layer upon layer of file systems, called UnionFS. Then, it is divided into two parts internally:

  • Bootfs (Boot File System) : BootFS is the lowest level of docker image, which mainly includes bootloader and kernel. Bootloader is used to boot and load the kernel. The bootfs file system is loaded when Linux starts. This layer, like a typical Linux /Unix system, contains the bootloader and kernel. After boot is loaded, the entire kernel is in memory. At this time, the use of memory has been transferred from bootFS to the kernel. At this time, the system will uninstall bootFS. The loading here can be understood as the process from the black screen to the operating system when our Windows computer is turned on.
  • Rootfs (Root file System) : On top of bootfs, contains the typical Linux system/dev、/proc、/bin、/etcAnd other standard directories and files.

    Rootfs is a variety of operating system distributions such as Ubuntu, Centos, and so on.

As shown in the figure:

Taking the Debian system as an example, the figure is divided into three processes from left to right:

  • Figure 1, starting state, downloads a Debian system.
  • In Figure 2, when you install an Emacs, you can see that a layer of Image has been added to figure 1.
  • In Figure 3, another Apache is installed, and another layer of Image is added on the basis of Figure 2.

Docker’s image is actually made up of layers of file systems. Bootfs is generally consistent across Linux distributions and rootfs vary, so different distributions can share bootfs.

In addition, the operating system on Docker is usually a compact version. The size of a centos image installed on a VM is more than 1 G, while the size of a centos image on Docker is only 200M. Because the underlying layer uses the host kernel directly, it only needs to provide rootfs, so rootFS can be small and only need to contain the most basic commands, tools, and libraries. This also makes it faster to boot because the most time-consuming boot loading process is gone.

Three, the principle of stratification

Now that we know how image loading works, let’s go back to how image layering works. As mentioned earlier, images are downloaded in layers, and some layers do not need to be downloaded again if they already exist. Let’s say I download a redis image.

The biggest benefit of this approach is resource sharing. For example, if multiple images are built from the same BASE image, the host only needs to keep one BASE image on disk and load only one BASE image in memory, so that all containers can use it. In addition, each layer of the mirror can be shared.

Docker Image Inspect can be used to view the layering of images, such as the redis image you just downloaded:

docker image inspect redis:latest 
Copy the code

All Docker images start with a base image layer. When modifying or adding new content, a new image layer will be created on top of the current image layer.

Such as:

I’m now going to make a mirror image.

  1. This image is based on Ubuntu Linux 16.04, which is the first layer of the image.
  2. Continuing to install the Python package creates a second image layer on top of the first.
  3. A third mirror layer will be created as the patch continues.

Note that while adding additional mirror layers, the mirror remains a combination of all the current mirrors, as shown below:

Here each mirror layer contains 3 files, while the mirror contains 6 files from 2 mirror layers.

Now, if file 5 in layer 2 needs an updated version. In this case, the file in the upper image overwrites the corresponding file in the lower image, causing the updated version in the file to be added to the image as a new image layer.

Docker realizes the stack of mirror layer by means of storage engine, and ensures that multiple mirror layers are displayed as a unified file system.

Commit mirroring

Now that we know how mirrors work, we can make one ourselves.

For example, now I’m pulling a Tomcat image as a base layer. After I start the image, I make some changes of my own in the container. I think my changes are good and the image is much more usable. So I need to save the state of the container by committing the image.

Docker commit -m=" Commit description "-a=" author" Container ID Target image name: version labelCopy the code

After running Tomcat, enter webapps and find that there is no project because it is a castrated version.

Now I copy everything from Webapps. dist into webapps.

Now I can access the project using IP :8080.

Now I submit the changed container.

Docker commit -m="pingguo first commit image" -a="pingguo" 03844FF66434 tomcatpingguo:1.0Copy the code

After successful submission,docker imagesThe image is saved locally.

Do you have a deeper understanding of the layering of mirrors by going back to your own mirror submission operation?