Table of Contents
1. Why do we need to optimize the image?
2. Several principles for building images
3. Simulate the source code in the virtual machine to compile nginx
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
Home Operation and Maintenance Docker What principles should docker images follow?

What principles should docker images follow?

Nov 25, 2021 pm 03:48 PM
docker mirror

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

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

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去除容器中所有不必要的东西
Copy after login

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 of this Website
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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP distributed system architecture and practice PHP distributed system architecture and practice May 04, 2024 am 10:33 AM

PHP distributed system architecture achieves scalability, performance, and fault tolerance by distributing different components across network-connected machines. The architecture includes application servers, message queues, databases, caches, and load balancers. The steps for migrating PHP applications to a distributed architecture include: Identifying service boundaries Selecting a message queue system Adopting a microservices framework Deployment to container management Service discovery

Pi Node Teaching: What is a Pi Node? How to install and set up Pi Node? Pi Node Teaching: What is a Pi Node? How to install and set up Pi Node? Mar 05, 2025 pm 05:57 PM

Detailed explanation and installation guide for PiNetwork nodes This article will introduce the PiNetwork ecosystem in detail - Pi nodes, a key role in the PiNetwork ecosystem, and provide complete steps for installation and configuration. After the launch of the PiNetwork blockchain test network, Pi nodes have become an important part of many pioneers actively participating in the testing, preparing for the upcoming main network release. If you don’t know PiNetwork yet, please refer to what is Picoin? What is the price for listing? Pi usage, mining and security analysis. What is PiNetwork? The PiNetwork project started in 2019 and owns its exclusive cryptocurrency Pi Coin. The project aims to create a one that everyone can participate

Agile development and operation of PHP microservice containerization Agile development and operation of PHP microservice containerization May 08, 2024 pm 02:21 PM

Answer: PHP microservices are deployed with HelmCharts for agile development and containerized with DockerContainer for isolation and scalability. Detailed description: Use HelmCharts to automatically deploy PHP microservices to achieve agile development. Docker images allow for rapid iteration and version control of microservices. The DockerContainer standard isolates microservices, and Kubernetes manages the availability and scalability of the containers. Use Prometheus and Grafana to monitor microservice performance and health, and create alarms and automatic repair mechanisms.

How to install deepseek How to install deepseek Feb 19, 2025 pm 05:48 PM

There are many ways to install DeepSeek, including: compile from source (for experienced developers) using precompiled packages (for Windows users) using Docker containers (for most convenient, no need to worry about compatibility) No matter which method you choose, Please read the official documents carefully and prepare them fully to avoid unnecessary trouble.

How to optimize the performance of Java functions through containerization? How to optimize the performance of Java functions through containerization? Apr 29, 2024 pm 03:09 PM

Containerization improves Java function performance in the following ways: Resource isolation - ensuring an isolated computing environment and avoiding resource contention. Lightweight - takes up less system resources and improves runtime performance. Fast startup - reduces function execution delays. Consistency - Decouple applications and infrastructure to ensure consistent behavior across environments.

How to use PHP CI/CD to iterate quickly? How to use PHP CI/CD to iterate quickly? May 08, 2024 pm 10:15 PM

Answer: Use PHPCI/CD to achieve rapid iteration, including setting up CI/CD pipelines, automated testing and deployment processes. Set up a CI/CD pipeline: Select a CI/CD tool, configure the code repository, and define the build pipeline. Automated testing: Write unit and integration tests and use testing frameworks to simplify testing. Practical case: Using TravisCI: install TravisCI, define the pipeline, enable the pipeline, and view the results. Implement continuous delivery: select deployment tools, define deployment pipelines, and automate deployment. Benefits: Improve development efficiency, reduce errors, and shorten delivery time.

Deploy JavaEE applications using Docker Containers Deploy JavaEE applications using Docker Containers Jun 05, 2024 pm 08:29 PM

Deploy Java EE applications using Docker containers: Create a Dockerfile to define the image, build the image, run the container and map the port, and then access the application in the browser. Sample JavaEE application: REST API interacts with database, accessible on localhost after deployment via Docker.

Questions and Answers on PHP Enterprise Application Microservice Architecture Design Questions and Answers on PHP Enterprise Application Microservice Architecture Design May 07, 2024 am 09:36 AM

Microservice architecture uses PHP frameworks (such as Symfony and Laravel) to implement microservices and follows RESTful principles and standard data formats to design APIs. Microservices communicate via message queues, HTTP requests, or gRPC, and use tools such as Prometheus and ELKStack for monitoring and troubleshooting.

See all articles