search
HomeOperation and MaintenanceDockerWhat principles should docker images follow?

Principles that docker images should follow: 1. Image minimization principle; it is necessary to select the most streamlined basic image, clean up the intermediate products of image construction, and reduce the number of image layers. 2. The principle of maximizing the build speed; make full use of the image to build the cache, and then use the built cache to speed up the image build. 3. Pay attention to optimizing network requests.

What principles should docker images follow?

The operating environment of this tutorial: linux5.9.8 system, docker-1.13.1 version, Dell G3 computer.

1. Why do we need to optimize the image?

As we continue to use the docker image, if we do not pay attention and optimize it during the process, the size of the image will become larger and larger
Many times when we use docker to deploy applications, we will find that the size of the image is at least 1G.
The increase in the size of the image will not only increase the cost of disk resources and network resources, but also affect the deployment efficiency of the application. The deployment time of the application will become longer and longer
Therefore, we need to reduce the size of the deployment image to speed up the deployment efficiency and reduce the resource overhead
As for the optimization of the image, it can be achieved by optimizing the dockerfile

2. Several principles for building images

(1) Image minimization principle

Choose the most streamlined basic image

Choose the smallest base image to effectively reduce the image size. Such as: alpine, busybox, etc.

Clean up the intermediate products of image construction

During the process of building the image, when the dockerfile instructions are executed, there is no need to delete the image. 's file.

If you use yum to install components, you can finally use yum clean all image to clean up unnecessary files or use the system rm command to delete unnecessary source files, etc.

Reduce the number of layers of the image

The image is a hierarchically stored file, and the image also has a certain limit on the number of layers. The current image has the highest layer number. It is layer 127.

If you don’t pay more attention, the image will become more and more bloated.

When using a dockerfile to build an image, each instruction in the dockerfile will generate a layer.

Therefore, you can reduce the number of layers in the final generated image by merging the mergeable instructions in the dockerfile.

For example: when using RUN to execute a shell command in a dockerfile, you can use "&&" to connect multiple commands.

Use the most basic image,

The smaller the image, the more streamlined it is

(2) The principle of maximizing the build speed

Make full use of the image build cache

We can use The built cache is used to speed up the image construction. Docker build will enable the cache by default. There are three key points for the cache to take effect.

The mirror parent layer has not changed, the build instructions remain unchanged, and the checksum of the added file is consistent.

As long as a build instruction meets these three conditions, this layer of image building will not be executed again, and it will directly use the results of the previous build.

After the image cache of a certain layer becomes invalid, the cache of the subsequent image layers will become invalid.

We should put the least changed part at the front of the Dockerfile so that we can make full use of the image cache.

There are commands WORKDIR, CMD, ENV, ADD, etc. in the dockerfile that may cause cache invalidation.

It is best to put these commands at the bottom of the dockerfile to maximize the use of the cache during the process of building the image. .

Delete unnecessary files in the build directory (default: the directory where the Dockerfile is located)

Write a .dockerignore file to filter unnecessary files during the build process or create a separate directory, and in the directory Only the files needed during the image building process exist.

Docker is divided into Docker engine (that is, server-side daemon) and client tools at runtime.

Docker's engine provides a set of REST APIs, called Docker Remote API,

And client tools such as docker commands interact with the Docker engine through this set of APIs. Thus completing various functions.

So, although on the surface it seems that we are executing various docker functions locally, in fact, everything is done on the server side (Docker engine) using remote calling. The docker build command builds the image. In fact, it is not built locally, but on the server, that is, in the Docker engine.

When building an image, Docker needs to prepare the context first and collect all required files into the process.

The default context includes all files in the Dockerfile directory.

(3) Pay attention to optimizing network requests

When we use some mirror sources or use urls on the Internet in dockerfile,

use some A relatively good open source site on the Internet can save time and reduce the failure rate.

3. Simulate the source code in the virtual machine to compile nginx

选择最精简的基础镜像
减少镜像的层数
清理镜像构建的中间产物
注意优化网络请求
尽量去用构建缓存

Start docker:

What principles should docker images follow?
View the image and delete it Useless image:
What principles should docker images follow?

What principles should docker images follow?

What principles should docker images follow?
First compile nginx from the source code. After you are familiar with the steps, you can run nginx in the container:

What principles should docker images follow?
What principles should docker images follow?
What principles should docker images follow?
What principles should docker images follow?
What principles should docker images follow?

What principles should docker images follow?
What principles should docker images follow?
##Close debug:
What principles should docker images follow?
What principles should docker images follow?
What principles should docker images follow?

What principles should docker images follow?
What principles should docker images follow?#View execution command
:
What principles should docker images follow?4. Optimization of the image

Phase construction of the image Next, we build the container with the rhel7 image and install the nginx source code package in the container. Build a new image with this container and optimize it

(1) Pass two packages to server1 on the real machine


What principles should docker images follow?
What principles should docker images follow?

What principles should docker images follow?

What principles should docker images follow?
What principles should docker images follow?Optimization idea: Put RUN in one line and reduce the number of mirror layers
:Write the Dockerfile as follows

What principles should docker images follow?
What principles should docker images follow?
What principles should docker images follow?

##Optimization idea: Use multi-stage build What principles should docker images follow?:
Dokcerfile As follows: First simulate the command line to turn off debug:



What principles should docker images follow?

What principles should docker images follow?

##Optimization idea: optimize from the bottom layer

What principles should docker images follow?

首先我们需要导入一个distroless和nginx镜像
distroless”镜像只包含应用程序及其运行时依赖项,不包含程序包管理器、shell以及在标准Linux发行版中可以找到的任何其他程序
用distroless去除容器中所有不必要的东西

1)从github网站查看例子:
What principles should docker images follow?
What principles should docker images follow?
What principles should docker images follow?
(2)从真机给server1发送东西
What principles should docker images follow?
(3)导入镜像

What principles should docker images follow?

(4)编写Dockerfile如下
What principles should docker images follow?
What principles should docker images follow?
(5)构建镜像并查看镜像大小

What principles should docker images follow?
(6)构建容器并测试
What principles should docker images follow?
查看IP并能正常访问到Nginx默认发布页,证明容器镜像可以正常使用,但只要内网可以访问:

What principles should docker images follow?
What principles should docker images follow?
按照查看桥接的工具:
What principles should docker images follow?
What principles should docker images follow?
查看桥接:
What principles should docker images follow?
做端口映射What principles should docker images follow?
可以通过外网访问了:
What principles should docker images follow?

推荐学习:《docker视频教程

The above is the detailed content of What principles should docker images follow?. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Docker and Linux: Building Portable ApplicationsDocker and Linux: Building Portable ApplicationsMay 03, 2025 am 12:17 AM

How to build portable applications with Docker and Linux? First, use Dockerfile to containerize the application, and then manage and deploy the container in a Linux environment. 1) Write a Dockerfile and package the application and its dependencies into a mirror. 2) Build and run containers on Linux using dockerbuild and dockerrun commands. 3) Manage multi-container applications through DockerCompose and define service dependencies. 4) Optimize the image size and resource configuration, enhance security, and improve application performance and portability.

Docker and Kubernetes: The Power of Container OrchestrationDocker and Kubernetes: The Power of Container OrchestrationMay 02, 2025 am 12:06 AM

Docker and Kubernetes improve application deployment and management efficiency through container orchestration. 1.Docker builds images through Dockerfile and runs containers to ensure application consistency. 2. Kubernetes manages containers through Pod, Deployment and Service to achieve automated deployment and expansion.

Docker vs. Kubernetes: Key Differences and SynergiesDocker vs. Kubernetes: Key Differences and SynergiesMay 01, 2025 am 12:09 AM

Docker and Kubernetes are leaders in containerization and orchestration. Docker focuses on container lifecycle management and is suitable for small projects; Kubernetes is good at container orchestration and is suitable for large-scale production environments. The combination of the two can improve development and deployment efficiency.

Docker and Linux: The Perfect PartnershipDocker and Linux: The Perfect PartnershipApr 30, 2025 am 12:02 AM

Docker and Linux are perfect matches because they can simplify the development and deployment of applications. 1) Docker uses Linux's namespaces and cgroups to implement container isolation and resource management. 2) Docker containers are more efficient than virtual machines, have faster startup speeds, and the mirrored hierarchical structure is easy to build and distribute. 3) On Linux, the installation and use of Docker is very simple, with only a few commands. 4) Through DockerCompose, you can easily manage and deploy multi-container applications.

Docker vs. Kubernetes: Deciding Which to UseDocker vs. Kubernetes: Deciding Which to UseApr 29, 2025 am 12:05 AM

The difference between Docker and Kubernetes is that Docker is a containerized platform suitable for small projects and development environments; Kubernetes is a container orchestration system suitable for large projects and production environments. 1.Docker simplifies application deployment and is suitable for small projects with limited resources. 2. Kubernetes provides automation and scalability capabilities, suitable for large projects that require efficient management.

Docker and Kubernetes: Building Scalable ApplicationsDocker and Kubernetes: Building Scalable ApplicationsApr 28, 2025 am 12:18 AM

Use Docker and Kubernetes to build scalable applications. 1) Create container images using Dockerfile, 2) Deployment and Service of Kubernetes through kubectl command, 3) Use HorizontalPodAutoscaler to achieve automatic scaling, thereby building an efficient and scalable application architecture.

Kubernetes and Docker: A Comparative AnalysisKubernetes and Docker: A Comparative AnalysisApr 27, 2025 am 12:05 AM

The main difference between Docker and Kubernetes is that Docker is used for containerization, while Kubernetes is used for container orchestration. 1.Docker provides a consistent environment to develop, test and deploy applications, and implement isolation and resource limitation through containers. 2. Kubernetes manages containerized applications, provides automated deployment, expansion and management functions, and supports load balancing and automatic scaling. The combination of the two can improve application deployment and management efficiency.

Running Docker on Linux: Installation and ConfigurationRunning Docker on Linux: Installation and ConfigurationApr 26, 2025 am 12:12 AM

Installing and configuring Docker on Linux requires ensuring that the system is 64-bit and kernel version 3.10 and above, use the command "sudoapt-getupdate" and install it with the command "sudoapt-getupdate" and verify it with "sudoapt-getupdate" and. Docker uses the namespace and control groups of the Linux kernel to achieve container isolation and resource limitation. The image is a read-only template, and the container can be modified. Examples of usage include running an Nginx server and creating images with custom Dockerfiles. common

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),