Docker from entry to application (4): Docker image

mirror image

Mirroring is a lightweight, executable independent software package used to package the software operating environment and software developed based on the operating environment . It contains everything needed to run a certain software, including code, libraries, environment variables, and configuration files.

Before we get to the bottom of mirroring, let's take a look at what a union file system (UnionFS) is

UnionFS (Union File System)

UnionFS (Union File System): Union File System (UnionFS) is a layered, lightweight and high-performance file system that supports modification of the file system as a submission to superimpose layer by layer. Mount different directories into the same virtual file system (unite several directories into a single virtual filesystem). The Union file system is the basis of the Docker image, and the image can be inherited by layering. Based on the base image (no parent image), various specific images can be made

Features : Load multiple file systems at the same time, but from the outside, only one file system can be seen. Joint loading will superimpose the file systems of each layer, so that the final file system will contain all the underlying files and directories

Docker image loading principle

The docker image actually consists of a layer-by-layer file system, this layered file system UnionFS.

bootfs (boot file system) mainly includes bootloader and kernel. The bootloader is mainly used to guide and load the kernel. When Linux is first started, the bootfs file system will be loaded. The bottom layer of the Docker image is bootfs. This layer is the same as our typical Linux/Unix system, including the boot loader and kernel. When the boot loading is complete, the entire kernel is in the memory. At this time, the right to use the memory has been transferred from the bootfs to the kernel. At this time, the system will also uninstall the bootfs.

Rootfs (root file system), on top of bootfs, contains standard directories and files such as /dev, /proc, /bin, /etc in the most typical Linux system. Rootfs is the release version of various operating systems, such as Ubuntu, Centos, etc.

image-20220711232926337

For a streamlined OS, the rootfs can be very small, and only need to include the most basic commands, tools and program libraries , because the underlying layer can directly use the Host's Kernel, and you only need to provide the rootfs. This is why some Docker system images are much smaller than commonly used system images. It can be seen that for different Linux distributions, the bootfs is basically the same, and the rootfs will be different, so usually different distributions can share the bootfs.

image-20220711234040312

layered mirroring

As shown in the figure below, mysql is taken as an example. When pulling a mirror, it is downloaded layer by layer.

image-20220711234603422

Why does the mirror adopt this layered structure

Best Benefits, Shared Resources

For example, if multiple images are built from the same base image, the host only needs to save one base image on disk and load one base image in memory to serve all containers. And every layer of the image can be shared

Mirror Features

Docker images are read-only . When the container starts, a new writable layer is loaded on top of the image. This layer is usually called the container layer , and everything below the container layer is called the image layer .

Build a new image based on the container and publish it to the image library (Docker image commit, push and pull)

Build a local mirror

  1. docker commit

dockers commitcommand to commit a copy of a container to make it a new image

docker commit -m="提交的描述信息" -a="作者" 容器Id 要创建的目标镜像名:[标签名]

Here we take the most commonly used tomcat as an example docker run -d -p 8080:8080 tomcatto create and start a tomcat container

image-20220718224536467

Visit the corresponding address and jump out of the tomcat default 404 interface, indicating that the container has been started and the downloaded tomcat does not contain the default page

image-20220718224830188

Execution docker exec -it b895400e6df8 /bin/bashEnter the tomcat container and open the terminal, enter the tomcat directory, create a test directory under the webapps directory, and edit an index.html page

image-20220718225513511

It is found that vi or vim is not installed in the container, so install vim first,

image-20220718225736462

Installation error, guess should be that the container needs to update the data source, after updating the data source, install vim

image-20220718231759205

After the installation is complete, create a new index.html and modify its content. After editing, visit the corresponding address

image-20220718232744019

Seeing the specific content of the edit shows that the modification is effective, but once the container is deleted at this time, after the container is created and started again according to the image tomcat, the test directory, that is, the files under the directory, will no longer exist. According to our modified container, it has been changed to Template, create a new tomcat mirror Hong_pro/mytomcat , and submit and save the file content in the form of mirroring. 执行命令docker commit -a="作者" -m="提交信息" 容器id 要创建的目标镜像名:[标签名]Here, note that the name of the target mirror to be created is removed from all lowercase letters. If the label name is left blank, it defaults to latest

image-20220718233717910

Execute after the creation is complete, docker imagesyou can see the edited new image, the size of the image is nearly 100M larger than the original tomcat

image-20220718234243127

Re-execute docker run -d -p 8090:8080 hong_pro/mytomcat, create and start the container based on the newly edited image, access the path of port 8090, and display the page we just created, indicating that the file has been created in the container

image-20220718235734575

Publish the local image to Alibaba Cloud

Log in to the Alibaba Cloud console, select the container and mirroring service, the personal version is free to use, and you can select the product to add if you have not added a product

image-20220720004619985

Select a personal instance to enter

image-20220720004738362

Create a mirror warehouse after selecting the corresponding region

image-20220720004836166

After entering the basic information, the next step is to select the local warehouse and create a mirror warehouse

image-20220720005038870

image-20220720005125034

After the creation is successful, you can enter the basic information of the warehouse. The operation guide includes basic mirroring operations, and you can operate the local mirroring according to the command prompt.

image-20220720005325285

For basic information, you can view the network address of the corresponding mirror, which can be used docker pushby bothdocker pull

image-20220720231010366

Execute docker login --username=你的用户名 <个人实例公网地址>After entering the password, log in to the mirror warehouse. If Login Succeeded appears, the login is successful. The password is the password set when the Alibaba Cloud container mirroring service was activated.

image-20220720004220764

Push the image to the Alibaba Cloud image warehouse

docker tag [imagesId] 仓库公网地址:[版本号]
docker push 仓库公网地址:[版本号]

image-20220720004109536

After the upload is complete, you can query the corresponding image version, and the image ID is consistent with the local image ID

image-20220720230357798

Download the image on Alibaba Cloud to the local

docker pull <镜像仓库地址>:[版本]

image-20220720230912496

The image version, the image ID is the same as the local image ID

[External link image transfer...(img-m59R10ml-1658330054693)]

Download the image on Alibaba Cloud to the local

docker pull <镜像仓库地址>:[版本]

[External link image transfer...(img-PFWdRMyV-1658330054694)]

Guess you like

Origin blog.csdn.net/Hong_pro/article/details/125902741